Skip to content

Commit

Permalink
Fix devops parameter group
Browse files Browse the repository at this point in the history
  • Loading branch information
SogoKato committed Jul 17, 2024
1 parent 07e0187 commit ab2693f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 35 deletions.
10 changes: 6 additions & 4 deletions docs/resources/devops_parameter_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ resource "nifcloud_devops_parameter_group" "example" {

The following arguments are supported:

* `description` - (Optional) Description of the DevOps parameter group.
* `name` - (Required) The name of the DevOps parameter group.
* `description` - (Optional) The description for the DevOps parameter group.
* `parameter` - (Optional) A list of parameters. see [parameter](#parameter)
* `sensitive_parameter` - (Optional) A list of parameters whose value should be masked. see [sensitive_parameter](#sensitive_parameter)
* `parameter` - (Optional) List of the DevOps parameters. see [parameter](#parameter)
* `sensitive_parameter` - (Optional) List of the DevOps parameters whose value should be masked. see [sensitive_parameter](#sensitive_parameter)

### parameter

Expand All @@ -66,9 +66,11 @@ The following arguments are supported:

### sensitive_parameter

* `name` - (Required) The name of the parameter. The allowed name is "smtp_password".
* `name` - (Required) The name of the parameter. Valid value is `smtp_password`.
* `value` - (Required) The value of the parameter.

Note: The calculated difference in the values of this parameter may be inaccurate due to API restrictions.

## Import

nifcloud_devops_parameter_group can be imported using the `parameter corresponding to id`, e.g.
Expand Down
68 changes: 66 additions & 2 deletions nifcloud/acc/devops_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,39 @@ func TestAcc_DevOpsParameterGroup(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", randName),
resource.TestCheckResourceAttr(resourceName, "description", "tfacc-memo"),
resource.TestCheckResourceAttr(resourceName, "parameter.#", "3"),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "smtp_user_name",
"value": "user1",
},
),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "gitlab_email_from",
"value": "[email protected]",
},
),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "gitlab_email_reply_to",
"value": "[email protected]",
},
),
resource.TestCheckResourceAttr(resourceName, "sensitive_parameter.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"sensitive_parameter.*",
map[string]string{
"name": "smtp_password",
"value": "mystrongpassword",
},
),
),
},
{
Expand All @@ -59,7 +91,39 @@ func TestAcc_DevOpsParameterGroup(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", randName+"-upd"),
resource.TestCheckResourceAttr(resourceName, "description", "tfacc-memo-upd"),
resource.TestCheckResourceAttr(resourceName, "parameter.#", "3"),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "smtp_user_name",
"value": "user101",
},
),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "gitlab_email_from",
"value": "[email protected]",
},
),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"parameter.*",
map[string]string{
"name": "gitlab_email_reply_to",
"value": "[email protected]",
},
),
resource.TestCheckResourceAttr(resourceName, "sensitive_parameter.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(
resourceName,
"sensitive_parameter.*",
map[string]string{
"name": "smtp_password",
"value": "mynewstrongpassword",
},
),
),
},
{
Expand Down Expand Up @@ -140,7 +204,7 @@ func testAccCheckDevOpsParameterGroupValues(group *types.ParameterGroup, rName s
func testAccCheckDevOpsParameterGroupValuesUpdated(group *types.ParameterGroup, rName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if nifcloud.ToString(group.ParameterGroupName) != rName+"-upd" {
return fmt.Errorf("bad parameter group name state, expected \"%s\", got: %#v", rName, nifcloud.ToString(group.ParameterGroupName))
return fmt.Errorf("bad parameter group name state, expected \"%s\", got: %#v", rName+"-upd", nifcloud.ToString(group.ParameterGroupName))
}

if nifcloud.ToString(group.Description) != "tfacc-memo-upd" {
Expand Down Expand Up @@ -198,7 +262,7 @@ func testAccDevOpsParameterGroupResourceDestroy(s *terraform.State) error {

if err != nil {
var awsErr smithy.APIError
if errors.As(err, &awsErr) && awsErr.ErrorCode() == "Client.InvalidParameterNotFound.ParameterGroupName" {
if errors.As(err, &awsErr) && awsErr.ErrorCode() == "Client.InvalidParameterNotFound.ParameterGroup" {
return nil
}
return fmt.Errorf("failed GetParameterGroup: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion nifcloud/resources/devops/devopsparametergroup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func createParameterGroup(ctx context.Context, d *schema.ResourceData, meta inte

res, err := svc.CreateParameterGroup(ctx, input)
if err != nil {
return diag.FromErr(fmt.Errorf("failed creating ParameterGroup: %s", err))
return diag.FromErr(fmt.Errorf("failed to create a DevOps parameter group: %s", err))
}

d.SetId(nifcloud.ToString(res.ParameterGroup.ParameterGroupName))
Expand Down
2 changes: 1 addition & 1 deletion nifcloud/resources/devops/devopsparametergroup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func deleteParameterGroup(ctx context.Context, d *schema.ResourceData, meta inte
input := expandDeleteParameterGroupInput(d)

if _, err := svc.DeleteParameterGroup(ctx, input); err != nil {
return diag.FromErr(fmt.Errorf("failed deleting ParameterGroup: %s", err))
return diag.FromErr(fmt.Errorf("failed to delete a DevOps parameter group: %s", err))
}

d.SetId("")
Expand Down
11 changes: 0 additions & 11 deletions nifcloud/resources/devops/devopsparametergroup/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package devopsparametergroup

import (
"reflect"
"strings"

"github.com/ettle/strcase"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -47,16 +46,6 @@ func expandUpdateParameterGroupParameters(configured []map[string]string) *types
if rawParam["name"] == "" {
continue
}
unsupportedSuffixes := []string{"2", "3", "4", "5"}
isUnsuppotedInSDK := false
for _, suffix := range unsupportedSuffixes {
if strings.HasSuffix(rawParam["name"], suffix) {
isUnsuppotedInSDK = true
}
}
if isUnsuppotedInSDK {
continue
}

name := strcase.ToPascal(rawParam["name"])
value := rawParam["value"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ func TestExpandUpdateParameterGroupParameters(t *testing.T) {
name: "expands the resource data",
args: configured,
want: &types.RequestParameters{
GitlabEmailFrom: nifcloud.String("test_value_01"),
GitlabEmailReplyTo: nifcloud.String("test_value_02"),
SmtpPassword: nifcloud.String("test_value_03"),
SmtpUserName: nifcloud.String("test_value_04"),
GitlabEmailFrom: nifcloud.String("test_value_01"),
GitlabEmailReplyTo: nifcloud.String("test_value_02"),
SmtpPassword: nifcloud.String("test_value_03"),
SmtpUserName: nifcloud.String("test_value_04"),
OmniauthProvidersSamlName2: nifcloud.String("test_value_06"),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func flatten(d *schema.ResourceData, res *devops.GetParameterGroupOutput) error
group := res.ParameterGroup

if nifcloud.ToString(group.ParameterGroupName) != d.Id() {
return fmt.Errorf("unable to find parameter group within: %#v", group)
return fmt.Errorf("unable to find the DevOps parameter group within: %#v", group)
}

if err := d.Set("name", group.ParameterGroupName); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions nifcloud/resources/devops/devopsparametergroup/flattener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import (

func TestFlatten(t *testing.T) {
rd := schema.TestResourceDataRaw(t, newSchema(), map[string]interface{}{
// define a parameter because the flattener only update configured parameters.
"parameter": []interface{}{
map[string]interface{}{
"name": "test_name_01",
"value": "",
},
},
})
rd.SetId("test_name")

wantRd := schema.TestResourceDataRaw(t, newSchema(), map[string]interface{}{
"name": "test_name",
"description": "test_description",
"parameter": []interface{}{
Expand All @@ -21,7 +32,7 @@ func TestFlatten(t *testing.T) {
},
},
})
rd.SetId("test_name")
wantRd.SetId("test_name")

wantNotFoundRd := schema.TestResourceDataRaw(t, newSchema(), map[string]interface{}{})

Expand Down Expand Up @@ -65,7 +76,7 @@ func TestFlatten(t *testing.T) {
},
},
},
want: rd,
want: wantRd,
},
{
name: "flattens the response even when the resource has been removed externally",
Expand Down Expand Up @@ -95,7 +106,6 @@ func TestFlatten(t *testing.T) {
}

assert.Equal(t, wantState.Attributes, gotState.Attributes)
assert.True(t, tt.want.Get("parameter").(*schema.Set).Equal(tt.args.d.Get("parameter")))
})
}
}
6 changes: 3 additions & 3 deletions nifcloud/resources/devops/devopsparametergroup/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
func readParameterGroup(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
svc := meta.(*client.Client).DevOps

getParameterGroupRes, err := svc.GetParameterGroup(ctx, expandGetParameterGroupInput(d))
res, err := svc.GetParameterGroup(ctx, expandGetParameterGroupInput(d))
if err != nil {
var awsErr smithy.APIError
if errors.As(err, &awsErr) && awsErr.ErrorCode() == "Client.InvalidParameterNotFound.ParameterGroup" {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("failed reading ParameterGroup: %s", err))
return diag.FromErr(fmt.Errorf("failed to read a DevOps parameter group: %s", err))
}

if err := flatten(d, getParameterGroupRes); err != nil {
if err := flatten(d, res); err != nil {
return diag.FromErr(err)
}

Expand Down
6 changes: 3 additions & 3 deletions nifcloud/resources/devops/devopsparametergroup/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the parameter group.",
Description: "The name of the DevOps parameter group.",
Required: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 63),
Expand All @@ -50,7 +50,7 @@ func newSchema() map[string]*schema.Schema {
},
"description": {
Type: schema.TypeString,
Description: "The description for the DB parameter group.",
Description: "Description of the DevOps parameter group.",
Optional: true,
ValidateDiagFunc: validator.StringRuneCountBetween(0, 255),
},
Expand Down Expand Up @@ -92,7 +92,7 @@ func newSchema() map[string]*schema.Schema {
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the parameter.",
Description: "The name of the parameter. Valid value is `smtp_password`.",
Required: true,
ValidateDiagFunc: func(v any, p cty.Path) diag.Diagnostics {
value := v.(string)
Expand Down
4 changes: 2 additions & 2 deletions nifcloud/resources/devops/devopsparametergroup/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func updateParameterGroup(ctx context.Context, d *schema.ResourceData, meta inte
// since ResourceData contains only configured parameters in tf file.
getParameterGroupRes, err := svc.GetParameterGroup(ctx, expandGetParameterGroupInput(d))
if err != nil {
return diag.FromErr(fmt.Errorf("failed reading ParameterGroup: %s", err))
return diag.FromErr(fmt.Errorf("failed to read a DevOps parameter group: %s", err))
}
currentParams := flattenParameters(getParameterGroupRes.ParameterGroup.Parameters)

Expand All @@ -51,7 +51,7 @@ func updateParameterGroup(ctx context.Context, d *schema.ResourceData, meta inte
input := expandUpdateParameterGroupInput(d, parametersToUpdate)

if _, err := svc.UpdateParameterGroup(ctx, input); err != nil {
return diag.FromErr(fmt.Errorf("failed updating ParameterGroup: %s", err))
return diag.FromErr(fmt.Errorf("failed to update a DevOps parameter group: %s", err))
}
}

Expand Down

0 comments on commit ab2693f

Please sign in to comment.