From efd7489f6f2a29e92a8a0ed43b67b9d64660fe03 Mon Sep 17 00:00:00 2001 From: Lajos Date: Tue, 13 Oct 2020 22:53:32 +0200 Subject: [PATCH] import --- configcat/resource_setting.go | 3 + configcat/resource_setting_value.go | 61 ++++++++++++++++--- configcat/resource_setting_value_bool_test.go | 10 --- docs/index.md | 1 - docs/resources/setting.md | 8 +++ docs/resources/setting_value.md | 11 +++- go.mod | 2 +- go.sum | 2 + samples/simple/root.tf | 1 - .../model_setting_data_model_ext.go | 14 +++++ .../model_setting_value_model_ext.go | 1 + vendor/modules.txt | 2 +- 12 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_data_model_ext.go diff --git a/configcat/resource_setting.go b/configcat/resource_setting.go index 166e58b4..a4211265 100644 --- a/configcat/resource_setting.go +++ b/configcat/resource_setting.go @@ -16,6 +16,9 @@ func resourceConfigCatSetting() *schema.Resource { ReadContext: resourceSettingRead, UpdateContext: resourceSettingUpdate, DeleteContext: resourceSettingDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ CONFIG_ID: &schema.Schema{ diff --git a/configcat/resource_setting_value.go b/configcat/resource_setting_value.go index 895b146f..d6f1a8e4 100644 --- a/configcat/resource_setting_value.go +++ b/configcat/resource_setting_value.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strconv" + "strings" sw "github.com/configcat/configcat-publicapi-go-client" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -16,6 +17,24 @@ func resourceConfigCatSettingValue() *schema.Resource { ReadContext: resourceSettingValueRead, UpdateContext: resourceSettingValueCreateOrUpdate, DeleteContext: resourceSettingValueDelete, + Importer: &schema.ResourceImporter{ + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + // d.Id() here is the last argument passed to the `terraform import RESOURCE_TYPE.RESOURCE_NAME RESOURCE_ID` command + // Here we use a function to parse the import ID (like the example above) to simplify our logic + environmentID, settingID, err := resourceConfigCatSettingValueParseId(d.Id()) + + if err != nil { + return nil, err + } + + d.Set(SETTING_ID, settingID) + d.Set(ENVIRONMENT_ID, environmentID) + d.Set(INIT_ONLY, false) + d.SetId(fmt.Sprintf("%s:%d", environmentID, settingID)) + + return []*schema.ResourceData{d}, nil + }, + }, Schema: map[string]*schema.Schema{ ENVIRONMENT_ID: &schema.Schema{ @@ -31,12 +50,6 @@ func resourceConfigCatSettingValue() *schema.Resource { ForceNew: true, }, - SETTING_TYPE: &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - SETTING_VALUE: &schema.Schema{ Type: schema.TypeString, Required: true, @@ -48,6 +61,11 @@ func resourceConfigCatSettingValue() *schema.Resource { Optional: true, }, + SETTING_TYPE: &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + ROLLOUT_RULES: &schema.Schema{ Type: schema.TypeList, Optional: true, @@ -139,6 +157,19 @@ func resourceSettingValueCreateOrUpdate(ctx context.Context, d *schema.ResourceD // Read the settingtype first so we know about the settingTypes settingTypeString := d.Get(SETTING_TYPE).(string) + if settingTypeString == "" { + setting, err := c.GetSetting(int32(settingID)) + if err != nil { + if _, ok := err.(NotFoundError); ok { + d.SetId("") + return diags + } + + return diag.FromErr(err) + } + + settingTypeString = fmt.Sprintf("%v", *setting.SettingType) + } settingValue, settingValueErr := getSettingValue(settingTypeString, d.Get(SETTING_VALUE).(string)) @@ -208,9 +239,10 @@ func resourceSettingValueReadInternal(ctx context.Context, d *schema.ResourceDat return err } - d.SetId(fmt.Sprintf("%s.%d", environmentID, settingID)) + d.SetId(fmt.Sprintf("%s:%d", environmentID, settingID)) d.Set(SETTING_VALUE, fmt.Sprintf("%v", *settingValue.Value)) + d.Set(SETTING_TYPE, settingValue.Setting.SettingType) d.Set(ROLLOUT_RULES, flattenRolloutRulesData(&settingValue.RolloutRules)) d.Set(ROLLOUT_PERCENTAGE_ITEMS, flattenRolloutPercentageItemsData(&settingValue.RolloutPercentageItems)) @@ -412,3 +444,18 @@ func getComparator(comparator string) (*sw.RolloutRuleComparator, error) { return nil, fmt.Errorf("could not parse Comparator: %s", comparator) } + +func resourceConfigCatSettingValueParseId(id string) (string, int32, error) { + parts := strings.SplitN(id, ":", 2) + + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return "", 0, fmt.Errorf("unexpected format of ID (%s), expected environmentID.settingID", id) + } + + settingID, err := strconv.ParseInt(parts[1], 10, 32) + if err != nil { + return "", 0, fmt.Errorf("unexpected format of ID (%s), expected environmentID.settingID. Error: %s", id, err) + } + + return parts[0], int32(settingID), nil +} diff --git a/configcat/resource_setting_value_bool_test.go b/configcat/resource_setting_value_bool_test.go index c182b030..a14abd83 100644 --- a/configcat/resource_setting_value_bool_test.go +++ b/configcat/resource_setting_value_bool_test.go @@ -18,7 +18,6 @@ func TestResourceSettingValueExistingFreeze(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" } ` @@ -26,7 +25,6 @@ func TestResourceSettingValueExistingFreeze(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "false" } ` @@ -65,7 +63,6 @@ func TestResourceSettingValueExistingNoFreeze(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false } @@ -74,7 +71,6 @@ func TestResourceSettingValueExistingNoFreeze(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "false" init_only = false } @@ -114,7 +110,6 @@ func TestResourceSettingValueRules(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false } @@ -123,7 +118,6 @@ func TestResourceSettingValueRules(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false rollout_rules { @@ -139,7 +133,6 @@ func TestResourceSettingValueRules(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false rollout_rules { @@ -202,7 +195,6 @@ func TestResourceSettingValuePercentageItems(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false } @@ -211,7 +203,6 @@ func TestResourceSettingValuePercentageItems(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false percentage_items { @@ -229,7 +220,6 @@ func TestResourceSettingValuePercentageItems(t *testing.T) { resource "configcat_setting_value" "test" { environment_id = "` + environmentID + `" setting_id = "` + settingID + `" - setting_type = "boolean" value = "true" init_only = false percentage_items { diff --git a/docs/index.md b/docs/index.md index 2873c563..e2dbd159 100644 --- a/docs/index.md +++ b/docs/index.md @@ -59,7 +59,6 @@ resource "configcat_setting" "setting" { resource "configcat_setting_value" "setting_value" { environment_id = data.configcat_environments.environments.environments.0.environment_id setting_id = configcat_setting.setting.id - setting_type = configcat_setting.setting.setting_type value = "false" } ``` diff --git a/docs/resources/setting.md b/docs/resources/setting.md index 08cea589..276ce8d5 100644 --- a/docs/resources/setting.md +++ b/docs/resources/setting.md @@ -40,6 +40,14 @@ output "setting_id" { * `id` - The unique Setting ID. +## Import + +Feature Flags/Settings can be imported using the SettingId. Get the SettingId using e.g. the [GetSettings API](https://api.configcat.com/docs/#operation/get-settings). + +``` +$ terraform import configcat_setting.example 1234 +``` + ## Used APIs: * [Read](https://api.configcat.com/docs/index.html#operation/get-setting) * [Create](https://api.configcat.com/docs/index.html#operation/create-setting) diff --git a/docs/resources/setting_value.md b/docs/resources/setting_value.md index 8bd0ff46..bad12d7e 100644 --- a/docs/resources/setting_value.md +++ b/docs/resources/setting_value.md @@ -27,7 +27,6 @@ data "configcat_settings" "settings" { resource "configcat_setting_value" "setting_value" { environment_id = data.configcat_environments.environments.environments.0.environment_id setting_id = data.configcat_settings.settings.settings.0.setting_id - setting_type = configcat_settings.settings.settings.0.setting_type value = "true" @@ -93,6 +92,16 @@ This prevents overriding the Feature Flag/Setting's modified values on the [Conf If you want to fully manage the Feature Flag/Setting's value from Terraform, set `init_only` argument to `false`. After setting the`init_only` argument to `false` each terraform run will update the Feature Flag/Setting's value to the state provided in Terraform. +## Import + +Feature Flag/Setting values can be imported using a combined EnvironmentID:SettingId ID. +Get the SettingId using e.g. the [GetSettings API](https://api.configcat.com/docs/#operation/get-settings). +Get the EnvironmentId using e.g. the [GetEnvironments API](https://api.configcat.com/docs/#operation/get-environments). + +``` +$ terraform import configcat_setting_value.example 08d86d63-2726-47cd-8bfc-59608ecb91e2:1234 +``` + ## Used APIs: * [Read](https://api.configcat.com/docs/#operation/get-setting-value) * [Update](https://api.configcat.com/docs/#operation/replace-setting-value) diff --git a/go.mod b/go.mod index 2cf2d2b1..5d144d33 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/configcat/terraform-provider-configcat go 1.15 require ( - github.com/configcat/configcat-publicapi-go-client v0.8.0 + github.com/configcat/configcat-publicapi-go-client v0.10.0 github.com/google/uuid v1.1.2 github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3 ) diff --git a/go.sum b/go.sum index 0ba40727..f3e199dc 100644 --- a/go.sum +++ b/go.sum @@ -86,6 +86,8 @@ github.com/configcat/configcat-publicapi-go-client v0.7.0 h1:7O/H+QBm6ZSgrub49f6 github.com/configcat/configcat-publicapi-go-client v0.7.0/go.mod h1:MudfrdzWKuoHJjNXXucQ4nBqqBBd12MSlQHhc0VvlBk= github.com/configcat/configcat-publicapi-go-client v0.8.0 h1:oMdb1B/5mVxlh3b8wKQQOz2yqnY+3FGmY97qXRS4+6g= github.com/configcat/configcat-publicapi-go-client v0.8.0/go.mod h1:MudfrdzWKuoHJjNXXucQ4nBqqBBd12MSlQHhc0VvlBk= +github.com/configcat/configcat-publicapi-go-client v0.10.0 h1:I2LeMkavWA4JUOpZBHDMjxp+0Pcycstihf0/zAasVRU= +github.com/configcat/configcat-publicapi-go-client v0.10.0/go.mod h1:MudfrdzWKuoHJjNXXucQ4nBqqBBd12MSlQHhc0VvlBk= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/samples/simple/root.tf b/samples/simple/root.tf index e9b36332..fffb9e2c 100644 --- a/samples/simple/root.tf +++ b/samples/simple/root.tf @@ -38,7 +38,6 @@ resource "configcat_setting" "setting" { resource "configcat_setting_value" "setting_value" { environment_id = data.configcat_environments.environments.environments.0.environment_id setting_id = configcat_setting.setting.id - setting_type = configcat_setting.setting.setting_type init_only = false value = "false" diff --git a/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_data_model_ext.go b/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_data_model_ext.go new file mode 100644 index 00000000..ab0265e0 --- /dev/null +++ b/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_data_model_ext.go @@ -0,0 +1,14 @@ +/* + * ConfigCat Public Management API + * + * **Base API URL**: https://api.configcat.com If you prefer the swagger documentation, you can find it here: [Swagger UI](https://api.configcat.com/swagger). The purpose of this API is to access the ConfigCat platform programmatically. You can **Create**, **Read**, **Update** and **Delete** any entities like **Feature Flags, Configs, Environments** or **Products** within ConfigCat. The API is based on HTTP REST, uses resource-oriented URLs, status codes and supports JSON and JSON+HAL format. Do not use this API for accessing and evaluating feature flag values. Use the [SDKs instead](https://configcat.com/docs/sdk-reference/overview). # OpenAPI Specification The complete specification is publicly available here: [swagger.json](v1/swagger.json). You can use it to generate client libraries in various languages with [OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) or [Swagger Codegen](https://swagger.io/tools/swagger-codegen/) to interact with this API. # Authentication This API uses the [Basic HTTP Authentication Scheme](https://en.wikipedia.org/wiki/Basic_access_authentication). # Throttling and rate limits All the rate limited API calls are returning information about the current rate limit period in the following HTTP headers: | Header | Description | | :- | :- | | X-Rate-Limit-Remaining | The maximum number of requests remaining in the current rate limit period. | | X-Rate-Limit-Reset | The time when the current rate limit period resets. | When the rate limit is exceeded by a request, the API returns with a `HTTP 429 - Too many requests` status along with a `Retry-After` HTTP header. + * + * API version: v1 + * Contact: support@configcat.com + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ +package configcatpublicapi + +type SettingDataModelSimple struct { + SettingType *SettingType `json:"settingType,omitempty"` +} diff --git a/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_value_model_ext.go b/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_value_model_ext.go index 88996a81..c0974a07 100644 --- a/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_value_model_ext.go +++ b/vendor/github.com/configcat/configcat-publicapi-go-client/model_setting_value_model_ext.go @@ -10,6 +10,7 @@ package configcatpublicapi type SettingValueSimpleModel struct { + Setting *SettingDataModelSimple `json:"setting,omitempty"` // The targeting rule collection. RolloutRules []RolloutRuleModel `json:"rolloutRules,omitempty"` // The percentage rule collection. diff --git a/vendor/modules.txt b/vendor/modules.txt index 112b6201..b3804d51 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -54,7 +54,7 @@ github.com/aws/aws-sdk-go/service/sts github.com/aws/aws-sdk-go/service/sts/stsiface # github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d github.com/bgentry/go-netrc/netrc -# github.com/configcat/configcat-publicapi-go-client v0.8.0 +# github.com/configcat/configcat-publicapi-go-client v0.10.0 ## explicit github.com/configcat/configcat-publicapi-go-client # github.com/davecgh/go-spew v1.1.1