Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/client/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,27 @@ func CreateDeployment(deploymentID string, displayName string, description strin
return respBody, err
}

// CreateDeploymentFromFile creates a deployment using raw JSON from a file.
func CreateDeploymentFromFile(deploymentID string, contents []byte) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeRegistryURL())
u.Path = path.Join(u.Path, "deployments")
if deploymentID != "" {
q := u.Query()
q.Set("deploymentId", deploymentID)
u.RawQuery = q.Encode()
}
respBody, err = apiclient.HttpClient(u.String(), string(contents))
return respBody, err
}

// UpdateDeploymentFromFile updates a deployment using raw JSON from a file.
func UpdateDeploymentFromFile(deploymentID string, contents []byte) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeRegistryURL())
u.Path = path.Join(u.Path, "deployments", deploymentID)
respBody, err = apiclient.HttpClient(u.String(), string(contents), "PATCH")
return respBody, err
}

func GetDeployment(deploymentID string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetApigeeRegistryURL())
u.Path = path.Join(u.Path, "deployments", deploymentID)
Expand Down
34 changes: 29 additions & 5 deletions internal/cmd/apihub/deployments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package deployments

import (
"fmt"
"internal/apiclient"
"internal/client/hub"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -32,6 +34,30 @@ var CrtCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

if filePath != "" {
var contents []byte
if contents, err = os.ReadFile(filePath); err != nil {
return err
}
_, err = hub.CreateDeploymentFromFile(deploymentID, contents)
return
}

// Validate required flags when not using a file
if displayName == "" {
return fmt.Errorf("required flag \"display-name\" not set")
}
if resourceURI == "" {
return fmt.Errorf("required flag \"resource-uri\" not set")
}
if len(endpoints) == 0 {
return fmt.Errorf("required flag \"endpoints\" not set")
}
if string(d) == "" {
return fmt.Errorf("required flag \"dep-type\" not set")
}

_, err = hub.CreateDeployment(deploymentID, displayName, description,
deploymentName, externalURI, resourceURI, endpoints, d, e, s)
return
Expand All @@ -45,6 +71,7 @@ var (
d hub.DeploymentType
e hub.EnvironmentType
s hub.SloType
filePath string
)

func init() {
Expand All @@ -65,9 +92,6 @@ func init() {
CrtCmd.Flags().Var(&d, "dep-type", "The type of deployment")
CrtCmd.Flags().Var(&e, "env-type", "The environment mapping to this deployment")
CrtCmd.Flags().Var(&s, "slo-type", "The SLO for this deployment")

_ = CrtCmd.MarkFlagRequired("display-name")
_ = CrtCmd.MarkFlagRequired("resource-uri")
_ = CrtCmd.MarkFlagRequired("endpoints")
_ = CrtCmd.MarkFlagRequired("dep-type")
CrtCmd.Flags().StringVarP(&filePath, "file", "f",
"", "Path to a JSON file containing the deployment definition (including custom attributes)")
}
15 changes: 15 additions & 0 deletions internal/cmd/apihub/deployments/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package deployments
import (
"internal/apiclient"
"internal/client/hub"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -32,11 +33,23 @@ var UpdateCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true

if updateFilePath != "" {
var contents []byte
if contents, err = os.ReadFile(updateFilePath); err != nil {
return err
}
_, err = hub.UpdateDeploymentFromFile(deploymentID, contents)
return
}

_, err = hub.UpdateDeployment(deploymentID, displayName, description, externalURI, resourceURI, endpoints, d, e, s)
return
},
}

var updateFilePath string

func init() {
UpdateCmd.Flags().StringVarP(&deploymentID, "id", "i",
"", "Deployment ID")
Expand All @@ -53,4 +66,6 @@ func init() {
UpdateCmd.Flags().Var(&d, "dep-type", "The type of deployment")
UpdateCmd.Flags().Var(&e, "env-type", "The environment mapping to this deployment")
UpdateCmd.Flags().Var(&s, "slo-type", "The SLO for this deployment")
UpdateCmd.Flags().StringVarP(&updateFilePath, "file", "f",
"", "Path to a JSON file containing the deployment definition to update (supports custom attributes)")
}