-
Notifications
You must be signed in to change notification settings - Fork 0
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 #6 from coherenceplatform/adamabdelaziz/wit-1271-a…
…dd-use-existing-image-override-command-to-cocli Service image override support
- Loading branch information
Showing
7 changed files
with
203 additions
and
6 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
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)", | ||
) | ||
} |
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 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) | ||
} |
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,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)") | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
0.0.3 | ||
0.1.0 |
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