Skip to content

Commit

Permalink
Merge pull request #6 from coherenceplatform/adamabdelaziz/wit-1271-a…
Browse files Browse the repository at this point in the history
…dd-use-existing-image-override-command-to-cocli

Service image override support
  • Loading branch information
zach-withcoherence authored Jan 24, 2024
2 parents d748240 + d595772 commit 63b32d0
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 6 deletions.
133 changes: 133 additions & 0 deletions cmd/cocli/environmentImageOverride.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package cocli

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

type UseExisting struct {
Image string `json:"image"`
Mode string `json:"mode"`
Tag string `json:"tag"`
}

type ProdImgConfigOverrideItem struct {
ServiceName string `json:"service_name"`
Prod struct {
UseExisting UseExisting `json:"use_existing"`
} `json:"prod"`
}

type DevImgConfigOverrideItem struct {
ServiceName string `json:"service_name"`
Dev struct {
UseExisting UseExisting `json:"use_existing"`
} `json:"dev"`
}

type ImageOverridePayload struct {
ConfigOverrideItems []interface{} `json:"config_override_items"`
}

var envImageOverrideEnvId int
var envImageOverrideServiceName string

var envImageOverrideImage string
var envImageOverrideTag string
var envImageOverrideMode string
var envImageOverrideDev bool

var environmentImageOverrideCmd = &cobra.Command{
Use: "set_service_image",
Short: "Override service image configuration",
Long: "Overrides the image configuration for services " +
"that use an existing image (not built by coherence)",
Run: func(cmd *cobra.Command, args []string) {
envImageOverridePayloadData := ImageOverridePayload{
ConfigOverrideItems: []interface{}{
ProdImgConfigOverrideItem{
ServiceName: envImageOverrideServiceName,
Prod: struct {
UseExisting UseExisting `json:"use_existing"`
}{
UseExisting: UseExisting{
Image: envImageOverrideImage,
Mode: envImageOverrideMode,
Tag: envImageOverrideTag,
},
},
},
},
}

if envImageOverrideDev == true {
envImageOverridePayloadData = ImageOverridePayload{
ConfigOverrideItems: []interface{}{
DevImgConfigOverrideItem{
ServiceName: envImageOverrideServiceName,
Dev: struct {
UseExisting UseExisting `json:"use_existing"`
}{
UseExisting: UseExisting{
Image: envImageOverrideImage,
Mode: envImageOverrideMode,
Tag: envImageOverrideTag,
},
},
},
},
}
}

payloadBytes, err := json.Marshal(envImageOverridePayloadData)
if err != nil {
panic(err)
}
payload := bytes.NewBuffer(payloadBytes)

envConfigOverrideUrl := fmt.Sprintf(
"https://%s%s/environments/%s/update_config_overrides",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
fmt.Sprint(envImageOverrideEnvId),
)
res, err := cocli.CoherenceApiRequest(
"PUT",
envConfigOverrideUrl,
payload,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}

func init() {
environmentImageOverrideCmd.Flags().IntVarP(&envImageOverrideEnvId, "environment_id", "e", 0, "Environment ID (required)")
environmentImageOverrideCmd.MarkFlagRequired("environment_id")

environmentImageOverrideCmd.Flags().StringVarP(&envImageOverrideServiceName, "service", "s", "", "Service name (required)")
environmentImageOverrideCmd.MarkFlagRequired("service")

environmentImageOverrideCmd.Flags().StringVarP(&envImageOverrideImage, "image", "i", "", "Image name without tag (optional)")
environmentImageOverrideCmd.Flags().StringVarP(&envImageOverrideTag, "tag", "t", "", "Image tag (optional)")
environmentImageOverrideCmd.Flags().StringVarP(&envImageOverrideMode, "mode", "m", "", "Image mode (optional)")

environmentImageOverrideCmd.Flags().BoolVarP(
&envImageOverrideDev,
"dev", "d", false,
"Only apply settings to the dev image config - this is the image used in workspaces (optional, default: false)",
)
}
15 changes: 15 additions & 0 deletions cmd/cocli/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cocli

import (
"github.com/spf13/cobra"
)

var environmentCmd = &cobra.Command{
Use: "environments",
Short: "Coherence environment management commands",
}

func init() {
environmentCmd.AddCommand(listEnvironmentsCmd)
environmentCmd.AddCommand(environmentImageOverrideCmd)
}
48 changes: 48 additions & 0 deletions cmd/cocli/listEnvironments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cocli

import (
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

var listEnvironmentsAppId int
var listEnvironmentsPageNumber int

var listEnvironmentsCmd = &cobra.Command{
Use: "list",
Short: "List static environments",
Long: "List all static environments (non branch tracking, e.g. production) for the specified application.",
Run: func(cmd *cobra.Command, args []string) {
environmentsListUrl := fmt.Sprintf(
"https://%s%s/environments?application_id=%s&page=%s",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
fmt.Sprint(listEnvironmentsAppId),
fmt.Sprint(listEnvironmentsPageNumber),
)
res, err := cocli.CoherenceApiRequest(
"GET",
environmentsListUrl,
nil,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}

func init() {
listEnvironmentsCmd.Flags().IntVarP(&listEnvironmentsAppId, "app_id", "a", 0, "App ID (required)")
listEnvironmentsCmd.MarkFlagRequired("app_id")
listEnvironmentsCmd.Flags().IntVarP(&listEnvironmentsPageNumber, "page", "p", 1, "Page number (optional - defaults to 1)")
}
4 changes: 2 additions & 2 deletions cmd/cocli/listFeatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var listFeaturesAppId int

var listFeaturesCmd = &cobra.Command{
Use: "list",
Short: "List all coherence applications",
Long: "List all coherence applications that are accessible by the currently authenticated user.",
Short: "List coherence features",
Long: "List all coherence features for the specified application.",
Run: func(cmd *cobra.Command, args []string) {
featuresListUrl := fmt.Sprintf(
"https://%s%s/features?application_id=%s",
Expand Down
1 change: 1 addition & 0 deletions cmd/cocli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func init() {
rootCmd.AddCommand(featureCmd)
rootCmd.AddCommand(currentUserCmd)
rootCmd.AddCommand(applicationCmd)
rootCmd.AddCommand(environmentCmd)
cocli.RunCliVersionCheck()
}
2 changes: 1 addition & 1 deletion cocli_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.1.0
6 changes: 3 additions & 3 deletions pkg/cocli/cocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type CocliConfig struct {
var devConfig = &CocliConfig{
ClientID: "O5AkI9iHd4Okb3DCmu1P0em4YXFjAPr5",
AuthDomain: "dev-mkiob4vl.us.auth0.com",
CoherenceDomain: "main.control-plane-review.coherence.coherencesites.com",
CoherenceDomain: "7ddf4b8e-682e-433f-b93f-fd24f4dda1ce-web.coherencedev.com",
}

// CoherenceDomain: "aa-external-cocli.control-plane-review.coherence.coherencesites.com",
// CoherenceDomain: "126bdeab-68f9-4d29-a22d-51f193623390-web.coherencedev.com",
// CoherenceDomain: "main.control-plane-review.coherence.coherencesites.com",

var prodConfig = &CocliConfig{
ClientID: "YfsRrC0cs29oEMc6Md9QtRopYLWa3785",
Expand All @@ -37,7 +37,7 @@ var prodConfig = &CocliConfig{
}

const (
cliVersion = "0.0.3"
cliVersion = "0.1.0"
credsFilename = "~/.cocli/.authtoken"
)

Expand Down

0 comments on commit 63b32d0

Please sign in to comment.