Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add command to migrate all configs from v1 to v2 #2903

Merged
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
2 changes: 1 addition & 1 deletion cli/COVERAGE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.96
20.82
2 changes: 1 addition & 1 deletion cli/cmd/COVERAGE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11.1
11.0
92 changes: 68 additions & 24 deletions cli/cmd/config_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/chanzuckerberg/happy/cli/pkg/hapi"
"github.com/chanzuckerberg/happy/shared/client"
apiclient "github.com/chanzuckerberg/happy/shared/hapi"
"github.com/chanzuckerberg/happy/shared/model"
"github.com/chanzuckerberg/happy/shared/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -16,6 +17,7 @@ import (

func init() {
configCmd.AddCommand(configMigrateCmd)
configCmd.AddCommand(configMigrateAllCmd)
}

var configMigrateCmd = &cobra.Command{
Expand Down Expand Up @@ -60,38 +62,80 @@ var configMigrateCmd = &cobra.Command{
return err
}

// Write the value to the v2 api
awsCredsProvider := hapi.NewAWSCredentialsProviderCLI(happyClient.AWSBackend)
creds, err := awsCredsProvider.GetCredentials(context.Background())
return migrateConfig(happyClient, result.Record)
},
}

var configMigrateAllCmd = &cobra.Command{
Use: "migrate-all",
Short: "migrate all config values from v1 to v2",
Long: "Migrate all configs for the given app, env, and stack from v1 to v2",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
checklist := util.NewValidationCheckList()
return util.ValidateEnvironment(cmd.Context(),
checklist.TerraformInstalled,
checklist.AwsInstalled,
)
},
RunE: func(cmd *cobra.Command, args []string) error {
happyClient, err := makeHappyClient(cmd, sliceName, "", []string{}, false)
if err != nil {
return errors.Wrap(err, "failed to get aws credentials")
return err
}

params := &apiclient.SetAppConfigParams{
AppName: happyClient.HappyConfig.App(),
Environment: happyClient.HappyConfig.GetEnv(),
Stack: &stack,
AwsProfile: *happyClient.HappyConfig.AwsProfile(),
AwsRegion: *happyClient.HappyConfig.AwsRegion(),
K8sNamespace: happyClient.HappyConfig.K8SConfig().Namespace,
K8sClusterId: happyClient.HappyConfig.K8SConfig().ClusterID,
XAwsAccessKeyId: b64.StdEncoding.EncodeToString([]byte(creds.AccessKeyID)),
XAwsSecretAccessKey: b64.StdEncoding.EncodeToString([]byte(creds.SecretAccessKey)),
XAwsSessionToken: creds.SessionToken, // SessionToken is already base64 encoded
}
apiv2 := hapi.MakeAPIClientV2(happyClient.HappyConfig)
resp, err := apiv2.SetAppConfigWithResponse(context.Background(), params, apiclient.SetAppConfigJSONRequestBody{Key: key, Value: result.Record.Value})
// Get the values from the v1 api
api := hapi.MakeAPIClient(happyClient.HappyConfig, happyClient.AWSBackend)
results, err := api.ListConfigs(happyClient.HappyConfig.App(), happyClient.HappyConfig.GetEnv(), stack)
if err != nil {
return err
}
if resp.HTTPResponse.StatusCode >= 400 {
return errors.New(string(resp.Body))
}

logrus.Info(messageWithStackSuffix(
fmt.Sprintf("successfully migrated app config with key '%s' in environment '%s'", resp.JSON200.Key, happyClient.HappyConfig.GetEnv()),
))
for _, record := range results.Records {
logrus.Info(messageWithStackSuffix(
fmt.Sprintf("migrating app config with key '%s' in environment '%s'", record.Key, happyClient.HappyConfig.GetEnv()),
))

err := migrateConfig(happyClient, record)
if err != nil {
return err
}
}
return nil
},
}

func migrateConfig(happyClient *HappyClient, record *model.ResolvedAppConfig) error {
// Write the value to the v2 api
awsCredsProvider := hapi.NewAWSCredentialsProviderCLI(happyClient.AWSBackend)
creds, err := awsCredsProvider.GetCredentials(context.Background())
if err != nil {
return errors.Wrap(err, "failed to get aws credentials")
}

params := &apiclient.SetAppConfigParams{
AppName: happyClient.HappyConfig.App(),
Environment: happyClient.HappyConfig.GetEnv(),
Stack: &record.Stack,
AwsProfile: *happyClient.HappyConfig.AwsProfile(),
AwsRegion: *happyClient.HappyConfig.AwsRegion(),
K8sNamespace: happyClient.HappyConfig.K8SConfig().Namespace,
K8sClusterId: happyClient.HappyConfig.K8SConfig().ClusterID,
XAwsAccessKeyId: b64.StdEncoding.EncodeToString([]byte(creds.AccessKeyID)),
XAwsSecretAccessKey: b64.StdEncoding.EncodeToString([]byte(creds.SecretAccessKey)),
XAwsSessionToken: creds.SessionToken, // SessionToken is already base64 encoded
}
apiv2 := hapi.MakeAPIClientV2(happyClient.HappyConfig)
resp, err := apiv2.SetAppConfigWithResponse(context.Background(), params, apiclient.SetAppConfigJSONRequestBody{Key: record.Key, Value: record.Value})
if err != nil {
return err
}
if resp.HTTPResponse.StatusCode >= 400 {
return errors.New(string(resp.Body))
}

logrus.Info(messageWithStackSuffix(
fmt.Sprintf("successfully migrated app config with key '%s' in environment '%s'", resp.JSON200.Key, happyClient.HappyConfig.GetEnv()),
))
return nil
}
Loading