Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
lajos88 committed Oct 13, 2020
1 parent 63ec0a2 commit efd7489
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 22 deletions.
3 changes: 3 additions & 0 deletions configcat/resource_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
61 changes: 54 additions & 7 deletions configcat/resource_setting_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
}
10 changes: 0 additions & 10 deletions configcat/resource_setting_value_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ func TestResourceSettingValueExistingFreeze(t *testing.T) {
resource "configcat_setting_value" "test" {
environment_id = "` + environmentID + `"
setting_id = "` + settingID + `"
setting_type = "boolean"
value = "true"
}
`
const settingValueResourceUpdated = `
resource "configcat_setting_value" "test" {
environment_id = "` + environmentID + `"
setting_id = "` + settingID + `"
setting_type = "boolean"
value = "false"
}
`
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
Expand Down
8 changes: 8 additions & 0 deletions docs/resources/setting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion docs/resources/setting_value.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 0 additions & 1 deletion samples/simple/root.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efd7489

Please sign in to comment.