-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from Tenderly/feature/spawn-devnet-rpc-url
add support for spawn-rpc api
- Loading branch information
Showing
9 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package devnet | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/tenderly/tenderly-cli/commands" | ||
) | ||
|
||
func init() { | ||
commands.RootCmd.AddCommand(CmdDevNet) | ||
} | ||
|
||
var CmdDevNet = &cobra.Command{ | ||
Use: "devnet", | ||
Short: "Tenderly DevNets.", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package devnet | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/tenderly/tenderly-cli/commands" | ||
"github.com/tenderly/tenderly-cli/config" | ||
"github.com/tenderly/tenderly-cli/userError" | ||
) | ||
|
||
var accountID string | ||
var projectSlug string | ||
var templateSlug string | ||
var accessKey string | ||
var token string | ||
|
||
func init() { | ||
cmdSpawnRpc.PersistentFlags().StringVar( | ||
&accountID, | ||
"account", | ||
"", | ||
"The Tenderly account username or organization slug. If not provided, the system will try to read 'account_id' from the 'tenderly.yaml' configuration file.", | ||
) | ||
cmdSpawnRpc.PersistentFlags().StringVar( | ||
&projectSlug, | ||
"project", | ||
"", | ||
"The DevNet project slug. If not provided, the system will try to read 'project_slug' from the 'tenderly.yaml' configuration file.", | ||
) | ||
cmdSpawnRpc.PersistentFlags().StringVar( | ||
&templateSlug, | ||
"template", | ||
"", | ||
"The DevNet template slug which is going to be applied when spawning the DevNet RPC.", | ||
) | ||
cmdSpawnRpc.PersistentFlags().StringVar( | ||
&accessKey, | ||
"access_key", | ||
"", | ||
"The Tenderly access key. If not provided, the system will try to read 'access_key' from the 'tenderly.yaml' configuration file.", | ||
) | ||
cmdSpawnRpc.PersistentFlags().StringVar( | ||
&token, | ||
"token", | ||
"", | ||
"The Tenderly JWT. If not provided, the system will try to read 'token' from the 'tenderly.yaml' configuration file.", | ||
) | ||
CmdDevNet.AddCommand(cmdSpawnRpc) | ||
} | ||
|
||
var cmdSpawnRpc = &cobra.Command{ | ||
Use: "spawn-rpc", | ||
Short: "Spawn DevNet RPC", | ||
Long: `Spawn DevNet RPC that represents the JSON-RPC endpoint for your DevNet`, | ||
Run: spawnRPCHandler, | ||
} | ||
|
||
func spawnRPCHandler(cmd *cobra.Command, args []string) { | ||
if accountID == "" { | ||
commands.CheckLogin() | ||
accountID = config.GetGlobalString(config.AccountID) | ||
} | ||
|
||
if accountID == "" { | ||
err := userError.NewUserError( | ||
errors.New("account not found"), | ||
"An account is required. Please log in using tenderly login or include the '--account' flag.", | ||
) | ||
userError.LogError(err) | ||
os.Exit(1) | ||
} | ||
|
||
if accessKey == "" { | ||
accessKey = config.GetGlobalString(config.AccessKey) | ||
} | ||
|
||
if token == "" { | ||
token = config.GetGlobalString(config.Token) | ||
} | ||
|
||
if accessKey == "" && token == "" { | ||
err := userError.NewUserError( | ||
errors.New("access key or token not found"), | ||
"An access key or token is required. Please log in using tenderly login or include the '--access_key' or '--token' flag.", | ||
) | ||
userError.LogError(err) | ||
os.Exit(1) | ||
} | ||
|
||
if projectSlug == "" { | ||
projectSlug = config.MaybeGetString(config.ProjectSlug) | ||
} | ||
|
||
if projectSlug == "" { | ||
err := userError.NewUserError( | ||
errors.New("project not found"), | ||
"No project was found. To set a project, use tenderly use project <project-slug> or include the '--project' flag.", | ||
) | ||
userError.LogError(err) | ||
os.Exit(1) | ||
} | ||
|
||
if templateSlug == "" { | ||
err := userError.NewUserError( | ||
errors.New("Missing required argument 'template'"), | ||
"The 'template' argument is required. Please include the '--template' flag and provide the DevNet template slug.", | ||
) | ||
userError.LogError(err) | ||
os.Exit(1) | ||
} | ||
|
||
rest := commands.NewRest() | ||
response, err := rest.DevNet.SpawnRPC(accountID, projectSlug, templateSlug, accessKey, token) | ||
if err != nil { | ||
logrus.Error("Failed to spawn RPC", err) | ||
return | ||
} | ||
logrus.Info(response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package call | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/tenderly/tenderly-cli/config" | ||
"github.com/tenderly/tenderly-cli/rest" | ||
"github.com/tenderly/tenderly-cli/rest/client" | ||
"github.com/tenderly/tenderly-cli/rest/payloads" | ||
) | ||
|
||
var _ rest.DevNetRoutes = (*DevNetCalls)(nil) | ||
|
||
type DevNetCalls struct{} | ||
|
||
func NewDevNetCalls() *DevNetCalls { | ||
return &DevNetCalls{} | ||
} | ||
|
||
type SpawnRPCRequest struct { | ||
Template string `json:"templateSlugOrId"` | ||
} | ||
|
||
func (rest *DevNetCalls) SpawnRPC( | ||
accountID string, | ||
projectID string, | ||
templateSlug string, | ||
accessKey string, | ||
token string, | ||
) (string, error) { | ||
req := &SpawnRPCRequest{ | ||
Template: templateSlug, | ||
} | ||
reqJson, err := json.Marshal(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
config.SetProjectConfig(config.AccessKey, accessKey) | ||
config.SetProjectConfig(config.Token, token) | ||
path := fmt.Sprintf("api/v1/account/%s/project/%s/devnet/container/spawn-rpc", accountID, projectID) | ||
resp := client.Request( | ||
"POST", | ||
path, | ||
reqJson, | ||
) | ||
var response *SpawnRPCResponse | ||
err = json.NewDecoder(resp).Decode(&response) | ||
if err != nil { | ||
return "", err | ||
} | ||
return response.URL, err | ||
} | ||
|
||
type SpawnRPCResponse struct { | ||
URL string `json:"url"` | ||
Error *payloads.ApiError `json:"error"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.