Skip to content

Commit

Permalink
enable commit while creating template/version
Browse files Browse the repository at this point in the history
  • Loading branch information
Hari Krishna Arla authored and hkantare committed Nov 19, 2024
1 parent 5c78eec commit 7f0333b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ func resourceIBMIAMAccessGroupTemplateCreate(context context.Context, d *schema.
}
version, _ := strconv.Atoi(*templateResponse.Version)

if d.Get("committed").(bool) {
commitTemplateOptions := &iamaccessgroupsv2.CommitTemplateOptions{}
commitTemplateOptions.SetTemplateID(*templateResponse.ID)
commitTemplateOptions.SetVersionNum(*templateResponse.Version)
commitTemplateOptions.SetIfMatch(response.Headers.Get("ETag"))
response, err = iamAccessGroupsClient.CommitTemplateWithContext(context, commitTemplateOptions)
if err != nil {
log.Printf("[DEBUG] CommitTemplateWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("CommitTemplateWithContext failed %s\n%s", err, response))
}
}
d.SetId(fmt.Sprintf("%s/%d", *templateResponse.ID, version))

return resourceIBMIAMAccessGroupTemplateVersionRead(context, d, meta)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestAccIBMIAMAccessGroupTemplateBasic(t *testing.T) {
testAccCheckIBMIAMAccessGroupTemplateVersionExists("ibm_iam_access_group_template.template", conf),
resource.TestCheckResourceAttr("ibm_iam_access_group_template.template", "name", name),
resource.TestCheckResourceAttr("ibm_iam_access_group_template.template", "group.0.name", agName),
resource.TestCheckResourceAttr("ibm_iam_access_group_template.template", "committed", "true"),
),
},
},
Expand Down Expand Up @@ -106,6 +107,7 @@ func testAccCheckIBMIAMAccessGroupTemplateConfigBasic(name string, agName string
group {
name = "%s"
}
committed = true
}
`, name, agName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ func resourceIBMIAMAccessGroupTemplateVersionCreate(context context.Context, d *
return diag.FromErr(fmt.Errorf("CreateTemplateVersionWithContext failed %s\n%s", err, response))
}

if d.Get("committed").(bool) {
commitTemplateOptions := &iamaccessgroupsv2.CommitTemplateOptions{}
commitTemplateOptions.SetTemplateID(*templateVersionResponse.ID)
commitTemplateOptions.SetVersionNum(*templateVersionResponse.Version)
commitTemplateOptions.SetIfMatch(response.Headers.Get("ETag"))
response, err = iamAccessGroupsClient.CommitTemplateWithContext(context, commitTemplateOptions)
if err != nil {
log.Printf("[DEBUG] CommitTemplateWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("CommitTemplateWithContext failed %s\n%s", err, response))
}
}

d.SetId(fmt.Sprintf("%s/%s", *templateVersionResponse.ID, *templateVersionResponse.Version))

return resourceIBMIAMAccessGroupTemplateVersionRead(context, d, meta)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccIBMIAMAccessGroupTemplateVersion(t *testing.T) {
resource.TestCheckResourceAttr("ibm_iam_access_group_template_version.template", "group.0.name", versionAGName),
resource.TestCheckResourceAttr("ibm_iam_access_group_template_version.template", "name", name),
resource.TestCheckResourceAttr("ibm_iam_access_group_template.template", "name", name),
resource.TestCheckResourceAttr("ibm_iam_access_group_template_version.template", "committed", "true"),
),
},
},
Expand All @@ -52,7 +53,7 @@ func TestAccIBMIAMAccessGroupTemplateVersionUpdateWithCommit(t *testing.T) {
CheckDestroy: testAccCheckIBMIAMAccessGroupTemplateVersionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMIAMAccessGroupTemplateVersionConfigBasic(name, agName, versionAGName),
Config: testAccCheckIBMIAMAccessGroupTemplateVersionConfigBasicWithoutCommit(name, agName, versionAGName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIAMAccessGroupTemplateVersionExists("ibm_iam_access_group_template_version.template", conf),
resource.TestCheckResourceAttr("ibm_iam_access_group_template.template", "group.0.name", agName),
Expand Down Expand Up @@ -87,6 +88,34 @@ func testAccCheckIBMIAMAccessGroupTemplateVersionConfigBasic(name string, agName
}
}
resource "ibm_iam_access_group_template_version" "template" {
template_id = ibm_iam_access_group_template.template.template_id
name = ibm_iam_access_group_template.template.name
description = "Testing4"
group {
name = "%s"
assertions {
action_controls {
add = false
remove = true
}
}
}
committed = true
}
`, name, agName, versionAGName)
}

func testAccCheckIBMIAMAccessGroupTemplateVersionConfigBasicWithoutCommit(name string, agName string, versionAGName string) string {
return fmt.Sprintf(`
resource "ibm_iam_access_group_template" "template" {
name = "%s"
description = "Test Terraform Description"
group {
name = "%s"
}
}
resource "ibm_iam_access_group_template_version" "template" {
template_id = ibm_iam_access_group_template.template.template_id
name = ibm_iam_access_group_template.template.name
Expand Down

0 comments on commit 7f0333b

Please sign in to comment.