Skip to content

Commit

Permalink
Promote codebase for version 0.0.5 (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: jatm80 <[email protected]>
  • Loading branch information
cloud-j-luna and jatm80 authored Oct 27, 2022
1 parent 9801a0b commit fc8e25e
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 93 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Transaction memo stating the Terraform provider was used and which version
### Fixed
- Bug where destroying deployments did not really close them
- Owner not being set in Go client for bid listing
- Issue on temporary folder on some OS
### Development
- Replaced HTTP API call for deployments with CLI

## [0.0.4] - 2022-08-17
### Added
Expand Down
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=joaoluna.com
NAMESPACE=cloud
NAME=akash
BINARY=terraform-provider-${NAME}
VERSION=0.0.4
VERSION=0.0.5
OS_ARCH=darwin_arm64
OS := $(shell uname -s | tr A-Z a-z)
HOSTNAME := $(shell hostname)
UNAME := $(shell uname -m)

ifeq ($(UNAME),x86_64)
ARCH=amd64
else ifeq ($(UNAME),i386)
ARCH=386
else ifeq ($(UNAME),arm64)
ARCH=arm64
endif

default: install

Expand All @@ -27,8 +37,8 @@ release:
#GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64

install: build
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS}_${ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS}_${ARCH}

test:
go test -i $(TEST) || exit 1
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ cd examples && terraform init && terraform apply --auto-approve
### Close the Deployment

```shell
./bin/akash tx deployment close --dseq 6958689 --owner $AKASH_ACCOUNT_ADDRESS --from $AKASH_KEY_NAME -y --gas=auto --gas-adjustment=1.15 --gas-prices=0.025uakt
akash tx deployment close --dseq 7878447 --owner $AKASH_ACCOUNT_ADDRESS --from $AKASH_KEY_NAME -y --gas=auto --gas-adjustment=1.15 --gas-prices=0.025uakt
```

### Get deployment details

```shell
./bin/akash provider lease-status --home ~/.akash --dseq <dseq> --provider <provider>
akash provider lease-status --home ~/.akash --dseq 7339802 --provider akash1e5g55l6dqwdjewq4zenl6u93t2mmy2603pungd
```

### Get logs

```shell
./bin/akash provider lease-logs --dseq <dseq> --provider <provider> --from "$AKASH_KEY_NAME"
akash provider lease-logs --dseq 7339802 --provider akash1e5g55l6dqwdjewq4zenl6u93t2mmy2603pungd --from "$AKASH_KEY_NAME"
```

## Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion akash/client/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (ak *AkashClient) GetBids(seqs Seqs, timeout time.Duration) (types.Bids, er
func queryBidList(ak *AkashClient, seqs Seqs) (types.Bids, error) {
cmd := cli.AkashCli(ak).Query().Market().Bid().List().
SetDseq(seqs.Dseq).SetGseq(seqs.Gseq).SetOseq(seqs.Oseq).
SetChainId(ak.Config.ChainId).SetNode(ak.Config.Node).OutputJson()
SetOwner(ak.Config.AccountAddress).SetChainId(ak.Config.ChainId).SetNode(ak.Config.Node).OutputJson()

bidsSliceWrapper := types.BidsSliceWrapper{}
if err := cmd.DecodeJson(&bidsSliceWrapper); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions akash/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (c AkashCommand) Deployment() AkashCommand {
return c.append("deployment")
}

func (c AkashCommand) Get() AkashCommand {
return c.append("get")
}

func (c AkashCommand) Create() AkashCommand {
return c.append("create")
}
Expand Down Expand Up @@ -141,6 +145,10 @@ func (c AkashCommand) SetKeyringBackend(keyringBackend string) AkashCommand {
return c.append("--keyring-backend").append(keyringBackend)
}

func (c AkashCommand) SetNote(note string) AkashCommand {
return c.append(fmt.Sprintf("--note=\"%s\"", note))
}

func (c AkashCommand) SetSignMode(mode string) AkashCommand {
supportedModes := map[string]bool{
"default": true,
Expand Down
16 changes: 16 additions & 0 deletions akash/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (c AkashCommand) AsCmd() *exec.Cmd {
)
}

type AkashErrorResponse struct {
RawLog string `json:"raw_log"`
}

func (c AkashCommand) Raw() ([]byte, error) {
cmd := c.AsCmd()

Expand All @@ -31,6 +35,18 @@ func (c AkashCommand) Raw() ([]byte, error) {
return c.Raw()
}

var akErr AkashErrorResponse
err := json.Unmarshal(out, &akErr)
if err != nil {
tflog.Error(c.ctx, fmt.Sprintf("Failure unmarshalling error: %s", err))
tflog.Debug(c.ctx, string(out))
}

if strings.Contains(akErr.RawLog, "out of gas in location") {
tflog.Info(c.ctx, "Transaction ran out of gas")
return nil, errors.New(akErr.RawLog)
}

return nil, errors.New(errb.String())
}

Expand Down
2 changes: 1 addition & 1 deletion akash/client/cli/groupings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cli

func (c AkashCommand) DefaultGas() AkashCommand {
return c.GasAuto().SetGasAdjustment(1.15).SetGasPrices().SetSignMode("amino-json")
return c.GasAuto().SetGasAdjustment(1.5).SetGasPrices().SetSignMode("amino-json")
}

func (c AkashCommand) SetSeqs(dseq string, gseq string, oseq string) AkashCommand {
Expand Down
9 changes: 7 additions & 2 deletions akash/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type AkashClient struct {
ctx context.Context
Config AkashConfiguration
ctx context.Context
Config AkashConfiguration
transactionNote string
}

type AkashConfiguration struct {
Expand All @@ -29,6 +30,10 @@ func (ak *AkashClient) GetPath() string {
return ak.Config.Path
}

func (ak *AkashClient) SetGlobalTransactionNote(note string) {
ak.transactionNote = note
}

func New(ctx context.Context, configuration AkashConfiguration) *AkashClient {
return &AkashClient{ctx: ctx, Config: configuration}
}
78 changes: 11 additions & 67 deletions akash/client/deployment.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package client

import (
"encoding/json"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"net/http"
"terraform-provider-akash/akash/client/cli"
"terraform-provider-akash/akash/client/types"
"time"
)

type Seqs struct {
Expand All @@ -17,74 +14,21 @@ type Seqs struct {
Oseq string
}

func (ak *AkashClient) GetDeployments() ([]map[string]interface{}, error) {
client := &http.Client{Timeout: 10 * time.Second}
address := ak.Config.AccountAddress

req, err := http.NewRequest("GET", fmt.Sprintf("%s/akash/deployment/v1beta2/deployments/list?filters.owner=%s", "https://akash.c29r3.xyz/api", address), nil)
if err != nil {
return nil, err
}

r, err := client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()

parsed := types.DeploymentResponse{}

err = json.NewDecoder(r.Body).Decode(&parsed)
if err != nil {
return nil, err
}

parsedDeployments := parsed.Deployments
deployments := make([]map[string]interface{}, 0)

for _, deployment := range parsedDeployments {
d := make(map[string]interface{})
d["deployment_state"] = deployment.DeploymentInfo.State
d["deployment_dseq"] = deployment.DeploymentInfo.DeploymentId.Dseq
d["deployment_owner"] = deployment.DeploymentInfo.DeploymentId.Owner

deployments = append(deployments, d)
}

return deployments, nil
func (ak *AkashClient) GetDeployments(owner string) ([]types.DeploymentId, error) {
panic("Not implemented")
}

func (ak *AkashClient) GetDeployment(dseq string, owner string) (map[string]interface{}, error) {
client := &http.Client{Timeout: 10 * time.Second}

req, err := http.NewRequest("GET", fmt.Sprintf("%s/akash/deployment/v1beta2/deployments/info?id.owner=%s&id.dseq=%s", "https://akash.c29r3.xyz/api", owner, dseq), nil)
if err != nil {
return nil, err
}

r, err := client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
func (ak *AkashClient) GetDeployment(dseq string, owner string) (types.Deployment, error) {
cmd := cli.AkashCli(ak).Query().Deployment().Get().SetOwner(owner).SetDseq(dseq).SetChainId(ak.Config.ChainId).
SetNode(ak.Config.Node).OutputJson()

deployment := types.Deployment{}

err = json.NewDecoder(r.Body).Decode(&deployment)
err := cmd.DecodeJson(&deployment)
if err != nil {
return nil, err
return types.Deployment{}, err
}

d := make(map[string]interface{})
d["deployment_state"] = deployment.DeploymentInfo.State
d["deployment_dseq"] = deployment.DeploymentInfo.DeploymentId.Dseq
d["deployment_owner"] = deployment.DeploymentInfo.DeploymentId.Owner
d["escrow_account_owner"] = deployment.EscrowAccount.Owner
d["escrow_account_state"] = deployment.EscrowAccount.State
d["escrow_account_balance_amount"] = deployment.EscrowAccount.Balance.Amount
d["escrow_account_balance_denom"] = deployment.EscrowAccount.Balance.Denom

return d, nil
return deployment, nil
}

func (ak *AkashClient) CreateDeployment(manifestLocation string) (Seqs, error) {
Expand All @@ -111,7 +55,7 @@ func (ak *AkashClient) CreateDeployment(manifestLocation string) (Seqs, error) {
func transactionCreateDeployment(ak *AkashClient, manifestLocation string) (types.TransactionEventAttributes, error) {
cmd := cli.AkashCli(ak).Tx().Deployment().Create().Manifest(manifestLocation).
DefaultGas().AutoAccept().SetFrom(ak.Config.KeyName).SetKeyringBackend(ak.Config.KeyringBackend).
SetChainId(ak.Config.ChainId).SetNode(ak.Config.Node).OutputJson()
SetNote(ak.transactionNote).SetChainId(ak.Config.ChainId).SetNode(ak.Config.Node).OutputJson()

transaction := types.Transaction{}
if err := cmd.DecodeJson(&transaction); err != nil {
Expand All @@ -129,7 +73,7 @@ func (ak *AkashClient) DeleteDeployment(dseq string, owner string) error {
cmd := cli.AkashCli(ak).Tx().Deployment().Close().
SetDseq(dseq).SetOwner(owner).SetFrom(ak.Config.KeyName).
DefaultGas().SetChainId(ak.Config.ChainId).SetKeyringBackend(ak.Config.KeyringBackend).
SetNode(ak.Config.Node).AutoAccept().OutputJson()
SetNote(ak.transactionNote).SetNode(ak.Config.Node).AutoAccept().OutputJson()

out, err := cmd.Raw()
if err != nil {
Expand All @@ -144,7 +88,7 @@ func (ak *AkashClient) DeleteDeployment(dseq string, owner string) error {
func (ak *AkashClient) UpdateDeployment(dseq string, manifestLocation string) error {
cmd := cli.AkashCli(ak).Tx().Deployment().Update().Manifest(manifestLocation).
SetDseq(dseq).SetFrom(ak.Config.KeyName).SetNode(ak.Config.Node).
SetKeyringBackend(ak.Config.KeyringBackend).SetChainId(ak.Config.ChainId).
SetNote(ak.transactionNote).SetKeyringBackend(ak.Config.KeyringBackend).SetChainId(ak.Config.ChainId).
GasAuto().SetGasAdjustment(1.5).SetGasPrices().SetSignMode("amino-json").AutoAccept().OutputJson()

out, err := cmd.Raw()
Expand Down
2 changes: 1 addition & 1 deletion akash/client/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (ak *AkashClient) CreateLease(seqs Seqs, provider string) (string, error) {
SetDseq(seqs.Dseq).SetGseq(seqs.Gseq).SetOseq(seqs.Oseq).
SetProvider(provider).SetOwner(ak.Config.AccountAddress).SetFrom(ak.Config.KeyName).
DefaultGas().SetChainId(ak.Config.ChainId).SetKeyringBackend(ak.Config.KeyringBackend).
AutoAccept().SetNode(ak.Config.Node).OutputJson()
SetNote(ak.transactionNote).AutoAccept().SetNode(ak.Config.Node).OutputJson()

out, err := cmd.Raw()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion akash/data_source_akash.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func dataSourceDeployments() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"deployment_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -44,7 +48,7 @@ func dataSourceDeploymentsRead(ctx context.Context, d *schema.ResourceData, m in
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

deployments, err := akash.GetDeployments()
deployments, err := akash.GetDeployments(akash.Config.AccountAddress)
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 3 additions & 2 deletions akash/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package akash
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io/ioutil"
"os"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

func CreateTemporaryDeploymentFile(ctx context.Context, sdl string) (string, error) {
timestamp := time.Now().UnixNano()
filename := fmt.Sprintf("%sdeployment-%d.yaml", os.TempDir(), timestamp)
filename := fmt.Sprintf("%s/deployment-%d.yaml", os.TempDir(), timestamp)
tflog.Debug(ctx, fmt.Sprintf("Creating temporary deployment file %s", filename))

err := ioutil.WriteFile(filename, []byte(sdl), 0666)
Expand Down
2 changes: 2 additions & 0 deletions akash/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}

akash := client.New(ctx, configuration)

akash.SetGlobalTransactionNote("Akash Terraform Provider - [provider=\"cloud-j-luna/akash\" version=\"0.0.5\"]")

return akash, diags
}

Expand Down
Loading

0 comments on commit fc8e25e

Please sign in to comment.