From 363fc2926f2adc5b3d778979c65effcfbab2e081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Mon, 26 Feb 2024 15:23:23 +0000 Subject: [PATCH] Setup for Object Storage API onboarding --- go.mod | 1 + go.sum | 2 + internal/cmd/config/set/set.go | 4 ++ internal/cmd/config/unset/unset.go | 7 ++++ internal/cmd/config/unset/unset_test.go | 11 ++++-- internal/pkg/config/config.go | 3 ++ .../services/object-storage/client/client.go | 37 +++++++++++++++++++ 7 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 internal/pkg/services/object-storage/client/client.go diff --git a/go.mod b/go.mod index 26958910..1e1d6fbc 100644 --- a/go.mod +++ b/go.mod @@ -51,6 +51,7 @@ require ( github.com/spf13/cast v1.6.0 // indirect github.com/stackitcloud/stackit-sdk-go/services/logme v0.10.0 github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.10.0 + github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.5 github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.10.0 github.com/stackitcloud/stackit-sdk-go/services/redis v0.10.0 github.com/subosito/gotenv v1.6.0 // indirect diff --git a/go.sum b/go.sum index 31e86b16..6ce1fcd2 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,8 @@ github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.10.0 h1:WxvySRZfkecOV github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.10.0/go.mod h1:GQ+UyuUOANO/kQOIhqLmMYRH34NbZShMrfmUY6hTSjo= github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.11.0 h1:Ys1WYHlM/5ev5WmM3Qn/FboIlKeXbQSOU2PYlMQ2C9A= github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.11.0/go.mod h1:n9bUnjBIAvpnyC5oNGFZKZ/mmoA9y7CBTFwi1KV62GA= +github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.5 h1:RbAorKhAoCPadWYp6tn6NZEWmafwfJvdNKW++SGc9Xo= +github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.5/go.mod h1:q6D+e173zF9ST5Nyj+StFlWTHjXutwCQ49mLBA1DrRA= github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.10.0 h1:Uv6r3tooqHgECoq/fGu4qZ/ojkf7H2lFH2NpsMhMxXM= github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.10.0/go.mod h1:VSwguERmACP2LSsk5OPLwTQxzt3CA8BasjB1qs+fjpI= github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.9.1 h1:41M0IWsHFmt2KvoY/4vwWYNG94Tx3U3diSlYdz0thJE= diff --git a/internal/cmd/config/set/set.go b/internal/cmd/config/set/set.go index 83fd0b90..a39b8df7 100644 --- a/internal/cmd/config/set/set.go +++ b/internal/cmd/config/set/set.go @@ -23,6 +23,7 @@ const ( logMeCustomEndpointFlag = "logme-custom-endpoint" mariaDBCustomEndpointFlag = "mariadb-custom-endpoint" mongoDBFlexCustomEndpointFlag = "mongodbflex-custom-endpoint" + objectStorageCustomEndpointFlag = "object-storage-custom-endpoint" openSearchCustomEndpointFlag = "opensearch-custom-endpoint" postgresFlexCustomEndpointFlag = "postgresflex-custom-endpoint" rabbitMQCustomEndpointFlag = "rabbitmq-custom-endpoint" @@ -95,6 +96,7 @@ func configureFlags(cmd *cobra.Command) { cmd.Flags().String(logMeCustomEndpointFlag, "", "LogMe API base URL, used in calls to this API") cmd.Flags().String(mariaDBCustomEndpointFlag, "", "MariaDB API base URL, used in calls to this API") cmd.Flags().String(mongoDBFlexCustomEndpointFlag, "", "MongoDB Flex API base URL, used in calls to this API") + cmd.Flags().String(objectStorageCustomEndpointFlag, "", "Object Storage API base URL, used in calls to this API") cmd.Flags().String(openSearchCustomEndpointFlag, "", "OpenSearch API base URL, used in calls to this API") cmd.Flags().String(postgresFlexCustomEndpointFlag, "", "PostgreSQL Flex API base URL, used in calls to this API") cmd.Flags().String(rabbitMQCustomEndpointFlag, "", "RabbitMQ API base URL, used in calls to this API") @@ -113,6 +115,8 @@ func configureFlags(cmd *cobra.Command) { cobra.CheckErr(err) err = viper.BindPFlag(config.MongoDBFlexCustomEndpointKey, cmd.Flags().Lookup(mongoDBFlexCustomEndpointFlag)) cobra.CheckErr(err) + err = viper.BindPFlag(config.ObjectStorageCustomEndpointKey, cmd.Flags().Lookup(objectStorageCustomEndpointFlag)) + cobra.CheckErr(err) err = viper.BindPFlag(config.OpenSearchCustomEndpointKey, cmd.Flags().Lookup(openSearchCustomEndpointFlag)) cobra.CheckErr(err) err = viper.BindPFlag(config.PostgresFlexCustomEndpointKey, cmd.Flags().Lookup(postgresFlexCustomEndpointFlag)) diff --git a/internal/cmd/config/unset/unset.go b/internal/cmd/config/unset/unset.go index 46733765..e35ffa47 100644 --- a/internal/cmd/config/unset/unset.go +++ b/internal/cmd/config/unset/unset.go @@ -25,6 +25,7 @@ const ( logMeCustomEndpointFlag = "logme-custom-endpoint" mariaDBCustomEndpointFlag = "mariadb-custom-endpoint" mongoDBFlexCustomEndpointFlag = "mongodbflex-custom-endpoint" + objectStorageCustomEndpointFlag = "object-storage-custom-endpoint" openSearchCustomEndpointFlag = "opensearch-custom-endpoint" postgresFlexCustomEndpointFlag = "postgresflex-custom-endpoint" rabbitMQCustomEndpointFlag = "rabbitmq-custom-endpoint" @@ -46,6 +47,7 @@ type inputModel struct { LogMeCustomEndpoint bool MariaDBCustomEndpoint bool MongoDBFlexCustomEndpoint bool + ObjectStorageCustomEndpoint bool OpenSearchCustomEndpoint bool PostgresFlexCustomEndpoint bool RabbitMQCustomEndpoint bool @@ -104,6 +106,9 @@ func NewCmd() *cobra.Command { if model.MongoDBFlexCustomEndpoint { viper.Set(config.MongoDBFlexCustomEndpointKey, "") } + if model.ObjectStorageCustomEndpoint { + viper.Set(config.ObjectStorageCustomEndpointKey, "") + } if model.OpenSearchCustomEndpoint { viper.Set(config.OpenSearchCustomEndpointKey, "") } @@ -149,6 +154,7 @@ func configureFlags(cmd *cobra.Command) { cmd.Flags().Bool(logMeCustomEndpointFlag, false, "LogMe API base URL. If unset, uses the default base URL") cmd.Flags().Bool(mariaDBCustomEndpointFlag, false, "MariaDB API base URL. If unset, uses the default base URL") cmd.Flags().Bool(mongoDBFlexCustomEndpointFlag, false, "MongoDB Flex API base URL. If unset, uses the default base URL") + cmd.Flags().Bool(objectStorageCustomEndpointFlag, false, "Object Storage API base URL. If unset, uses the default base URL") cmd.Flags().Bool(openSearchCustomEndpointFlag, false, "OpenSearch API base URL. If unset, uses the default base URL") cmd.Flags().Bool(postgresFlexCustomEndpointFlag, false, "PostgreSQL Flex API base URL. If unset, uses the default base URL") cmd.Flags().Bool(rabbitMQCustomEndpointFlag, false, "RabbitMQ API base URL. If unset, uses the default base URL") @@ -170,6 +176,7 @@ func parseInput(cmd *cobra.Command) *inputModel { LogMeCustomEndpoint: flags.FlagToBoolValue(cmd, logMeCustomEndpointFlag), MariaDBCustomEndpoint: flags.FlagToBoolValue(cmd, mariaDBCustomEndpointFlag), MongoDBFlexCustomEndpoint: flags.FlagToBoolValue(cmd, mongoDBFlexCustomEndpointFlag), + ObjectStorageCustomEndpoint: flags.FlagToBoolValue(cmd, objectStorageCustomEndpointFlag), OpenSearchCustomEndpoint: flags.FlagToBoolValue(cmd, openSearchCustomEndpointFlag), PostgresFlexCustomEndpoint: flags.FlagToBoolValue(cmd, postgresFlexCustomEndpointFlag), RabbitMQCustomEndpoint: flags.FlagToBoolValue(cmd, rabbitMQCustomEndpointFlag), diff --git a/internal/cmd/config/unset/unset_test.go b/internal/cmd/config/unset/unset_test.go index e2a41e5a..ca9b358c 100644 --- a/internal/cmd/config/unset/unset_test.go +++ b/internal/cmd/config/unset/unset_test.go @@ -16,6 +16,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]bool)) map[string]bool dnsCustomEndpointFlag: true, logMeCustomEndpointFlag: true, mariaDBCustomEndpointFlag: true, + objectStorageCustomEndpointFlag: true, openSearchCustomEndpointFlag: true, rabbitMQCustomEndpointFlag: true, redisCustomEndpointFlag: true, @@ -38,12 +39,13 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { DNSCustomEndpoint: true, LogMeCustomEndpoint: true, MariaDBCustomEndpoint: true, - ServiceAccountCustomEndpoint: true, - SKECustomEndpoint: true, - ResourceManagerCustomEndpoint: true, + ObjectStorageCustomEndpoint: true, OpenSearchCustomEndpoint: true, - RedisCustomEndpoint: true, RabbitMQCustomEndpoint: true, + RedisCustomEndpoint: true, + ResourceManagerCustomEndpoint: true, + ServiceAccountCustomEndpoint: true, + SKECustomEndpoint: true, } for _, mod := range mods { mod(model) @@ -76,6 +78,7 @@ func TestParseInput(t *testing.T) { model.DNSCustomEndpoint = false model.LogMeCustomEndpoint = false model.MariaDBCustomEndpoint = false + model.ObjectStorageCustomEndpoint = false model.OpenSearchCustomEndpoint = false model.RabbitMQCustomEndpoint = false model.RedisCustomEndpoint = false diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 81b27c1c..f8c43ec9 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -21,6 +21,7 @@ const ( LogMeCustomEndpointKey = "logme_custom_endpoint" MariaDBCustomEndpointKey = "mariadb_custom_endpoint" MongoDBFlexCustomEndpointKey = "mongodbflex_custom_endpoint" + ObjectStorageCustomEndpointKey = "object_storage_custom_endpoint" OpenSearchCustomEndpointKey = "opensearch_custom_endpoint" PostgresFlexCustomEndpointKey = "postgresflex_custom_endpoint" RabbitMQCustomEndpointKey = "rabbitmq_custom_endpoint" @@ -50,6 +51,7 @@ var ConfigKeys = []string{ DNSCustomEndpointKey, LogMeCustomEndpointKey, MariaDBCustomEndpointKey, + ObjectStorageCustomEndpointKey, OpenSearchCustomEndpointKey, PostgresFlexCustomEndpointKey, ResourceManagerEndpointKey, @@ -126,6 +128,7 @@ func setConfigDefaults() { viper.SetDefault(DNSCustomEndpointKey, "") viper.SetDefault(AuthorizationCustomEndpointKey, "") viper.SetDefault(MongoDBFlexCustomEndpointKey, "") + viper.SetDefault(ObjectStorageCustomEndpointKey, "") viper.SetDefault(OpenSearchCustomEndpointKey, "") viper.SetDefault(PostgresFlexCustomEndpointKey, "") viper.SetDefault(ResourceManagerEndpointKey, "") diff --git a/internal/pkg/services/object-storage/client/client.go b/internal/pkg/services/object-storage/client/client.go new file mode 100644 index 00000000..6091f08a --- /dev/null +++ b/internal/pkg/services/object-storage/client/client.go @@ -0,0 +1,37 @@ +package client + +import ( + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" + "github.com/stackitcloud/stackit-cli/internal/pkg/config" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" +) + +func ConfigureClient(cmd *cobra.Command) (*objectstorage.APIClient, error) { + var err error + var apiClient *objectstorage.APIClient + var cfgOptions []sdkConfig.ConfigurationOption + + authCfgOption, err := auth.AuthenticationConfig(cmd, auth.AuthorizeUser) + if err != nil { + return nil, &errors.AuthError{} + } + cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion("eu01")) + + customEndpoint := viper.GetString(config.ObjectStorageCustomEndpointKey) + + if customEndpoint != "" { + cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint)) + } + + apiClient, err = objectstorage.NewAPIClient(cfgOptions...) + if err != nil { + return nil, &errors.AuthError{} + } + + return apiClient, nil +}