Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/44523.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_ssmcontacts_contact: Add `rotation_ids` argument to support `ONCALL_SCHEDULE` contact type
```

```release-note:enhancement
data-source/aws_ssmcontacts_contact: Add `rotation_ids` attribute to support `ONCALL_SCHEDULE` contact type
```
42 changes: 37 additions & 5 deletions internal/service/ssmcontacts/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -49,6 +50,13 @@ func ResourceContact() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"rotation_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
names.AttrType: {
Type: schema.TypeString,
Required: true,
Expand All @@ -68,12 +76,23 @@ func resourceContactCreate(ctx context.Context, d *schema.ResourceData, meta any
var diags diag.Diagnostics
client := meta.(*conns.AWSClient).SSMContactsClient(ctx)

contactType := types.ContactType(d.Get(names.AttrType).(string))

input := &ssmcontacts.CreateContactInput{
Alias: aws.String(d.Get(names.AttrAlias).(string)),
DisplayName: aws.String(d.Get(names.AttrDisplayName).(string)),
Plan: &types.Plan{Stages: []types.Stage{}},
Tags: getTagsIn(ctx),
Type: types.ContactType(d.Get(names.AttrType).(string)),
Type: contactType,
}

if contactType == types.ContactTypeOncallSchedule {
plan := &types.Plan{}
if v, ok := d.GetOk("rotation_ids"); ok {
plan.RotationIds = flex.ExpandStringValueList(v.([]any))
}
input.Plan = plan
} else {
input.Plan = &types.Plan{Stages: []types.Stage{}}
}

output, err := client.CreateContact(ctx, input)
Expand Down Expand Up @@ -117,10 +136,23 @@ func resourceContactUpdate(ctx context.Context, d *schema.ResourceData, meta any
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SSMContactsClient(ctx)

if d.HasChanges(names.AttrDisplayName) {
if d.HasChanges(names.AttrDisplayName, "rotation_ids") {
contactType := types.ContactType(d.Get(names.AttrType).(string))

in := &ssmcontacts.UpdateContactInput{
ContactId: aws.String(d.Id()),
DisplayName: aws.String(d.Get(names.AttrDisplayName).(string)),
ContactId: aws.String(d.Id()),
}

if d.HasChange(names.AttrDisplayName) {
in.DisplayName = aws.String(d.Get(names.AttrDisplayName).(string))
}

if d.HasChange("rotation_ids") && contactType == types.ContactTypeOncallSchedule {
plan := &types.Plan{}
if v, ok := d.GetOk("rotation_ids"); ok {
plan.RotationIds = flex.ExpandStringValueList(v.([]any))
}
in.Plan = plan
}

_, err := conn.UpdateContact(ctx, in)
Expand Down
7 changes: 7 additions & 0 deletions internal/service/ssmcontacts/contact_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func DataSourceContact() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"rotation_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
names.AttrType: {
Type: schema.TypeString,
Computed: true,
Expand Down
75 changes: 75 additions & 0 deletions internal/service/ssmcontacts/contact_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ func testAccContactDataSource_basic(t *testing.T) {
})
}

func testAccContactDataSource_oncallSchedule(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_ssmcontacts_contact.test"
dataSourceName := "data.aws_ssmcontacts_contact.test"

resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccContactPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.SSMContactsServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccContactDataSourceConfig_oncallSchedule(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, names.AttrARN, dataSourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(resourceName, names.AttrAlias, dataSourceName, names.AttrAlias),
resource.TestCheckResourceAttrPair(resourceName, names.AttrType, dataSourceName, names.AttrType),
resource.TestCheckResourceAttrPair(resourceName, names.AttrDisplayName, dataSourceName, names.AttrDisplayName),
resource.TestCheckResourceAttrPair(resourceName, "rotation_ids.#", dataSourceName, "rotation_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "rotation_ids.0", dataSourceName, "rotation_ids.0"),
),
},
},
})
}

func testAccContactDataSourceConfig_base() string {
return fmt.Sprintf(`
resource "aws_ssmincidents_replication_set" "test" {
Expand Down Expand Up @@ -79,3 +112,45 @@ data "aws_ssmcontacts_contact" "contact_one" {
}
`, alias))
}

func testAccContactDataSourceConfig_oncallSchedule(alias string) string {
return acctest.ConfigCompose(
testAccContactDataSourceConfig_base(),
fmt.Sprintf(`
resource "aws_ssmcontacts_contact" "test_contact" {
alias = "%[1]s-contact"
type = "PERSONAL"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_rotation" "test" {
contact_ids = [aws_ssmcontacts_contact.test_contact.arn]
name = %[1]q
recurrence {
number_of_on_calls = 1
recurrence_multiplier = 1
daily_settings {
hour_of_day = 9
minute_of_hour = 0
}
}
time_zone_id = "America/Los_Angeles"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_contact" "test" {
alias = %[1]q
display_name = %[1]q
type = "ONCALL_SCHEDULE"
rotation_ids = [aws_ssmcontacts_rotation.test.arn]

depends_on = [aws_ssmincidents_replication_set.test]
}

data "aws_ssmcontacts_contact" "test" {
arn = aws_ssmcontacts_contact.test.arn
}
`, alias))
}
153 changes: 153 additions & 0 deletions internal/service/ssmcontacts/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,57 @@ func testAccContact_updateDisplayName(t *testing.T) {
})
}

func testAccContact_oncallSchedule(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_ssmcontacts_contact.test"

resource.Test(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccContactPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.SSMContactsServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckContactDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccContactConfig_oncallSchedule(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContactExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, names.AttrAlias, rName),
resource.TestCheckResourceAttr(resourceName, names.AttrType, "ONCALL_SCHEDULE"),
resource.TestCheckResourceAttr(resourceName, "rotation_ids.#", "1"),
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "ssm-contacts", regexache.MustCompile(`contact/.+$`)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContactConfig_oncallScheduleUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContactExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, names.AttrAlias, rName),
resource.TestCheckResourceAttr(resourceName, names.AttrType, "ONCALL_SCHEDULE"),
resource.TestCheckResourceAttr(resourceName, "rotation_ids.#", "2"),
acctest.MatchResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "ssm-contacts", regexache.MustCompile(`contact/.+$`)),
),
},
{
Config: testAccContactConfig_none(),
Check: testAccCheckContactDestroy(ctx),
},
},
})
}

func testAccCheckContactDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).SSMContactsClient(ctx)
Expand Down Expand Up @@ -383,3 +434,105 @@ resource "aws_ssmcontacts_contact" "contact_one" {
}
`, alias, displayName))
}

func testAccContactConfig_oncallSchedule(alias string) string {
return acctest.ConfigCompose(
testAccContactConfig_base(),
fmt.Sprintf(`
resource "aws_ssmcontacts_contact" "test_contact" {
alias = "%[1]s-contact"
type = "PERSONAL"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_rotation" "test" {
contact_ids = [aws_ssmcontacts_contact.test_contact.arn]
name = %[1]q
recurrence {
number_of_on_calls = 1
recurrence_multiplier = 1
daily_settings {
hour_of_day = 9
minute_of_hour = 0
}
}
time_zone_id = "America/Los_Angeles"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_contact" "test" {
alias = %[1]q
display_name = %[1]q
type = "ONCALL_SCHEDULE"
rotation_ids = [aws_ssmcontacts_rotation.test.arn]

depends_on = [aws_ssmincidents_replication_set.test]
}
`, alias))
}

func testAccContactConfig_oncallScheduleUpdated(alias string) string {
return acctest.ConfigCompose(
testAccContactConfig_base(),
fmt.Sprintf(`
resource "aws_ssmcontacts_contact" "test_contact" {
alias = "%[1]s-contact"
type = "PERSONAL"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_contact" "test_contact_2" {
alias = "%[1]s-contact-2"
type = "PERSONAL"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_rotation" "test" {
contact_ids = [aws_ssmcontacts_contact.test_contact.arn]
name = %[1]q
recurrence {
number_of_on_calls = 1
recurrence_multiplier = 1
daily_settings {
hour_of_day = 9
minute_of_hour = 0
}
}
time_zone_id = "America/Los_Angeles"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_rotation" "test_2" {
contact_ids = [aws_ssmcontacts_contact.test_contact_2.arn]
name = "%[1]s-2"
recurrence {
number_of_on_calls = 1
recurrence_multiplier = 1
daily_settings {
hour_of_day = 14
minute_of_hour = 30
}
}
time_zone_id = "America/New_York"

depends_on = [aws_ssmincidents_replication_set.test]
}

resource "aws_ssmcontacts_contact" "test" {
alias = %[1]q
display_name = %[1]q
type = "ONCALL_SCHEDULE"
rotation_ids = [
aws_ssmcontacts_rotation.test.arn,
aws_ssmcontacts_rotation.test_2.arn
]

depends_on = [aws_ssmincidents_replication_set.test]
}
`, alias))
}
3 changes: 3 additions & 0 deletions internal/service/ssmcontacts/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func setContactResourceData(d *schema.ResourceData, getContactOutput *ssmcontact
d.Set(names.AttrAlias, getContactOutput.Alias)
d.Set(names.AttrType, getContactOutput.Type)
d.Set(names.AttrDisplayName, getContactOutput.DisplayName)
if getContactOutput.Plan != nil {
d.Set("rotation_ids", getContactOutput.Plan.RotationIds)
}

return nil
}
Expand Down
6 changes: 4 additions & 2 deletions internal/service/ssmcontacts/ssmcontacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ func TestAccSSMContacts_serial(t *testing.T) {
"updateDisplayName": testAccContact_updateDisplayName,
"tags": testAccSSMContactsContact_tagsSerial,
"updateType": testAccContact_updateType,
"oncallSchedule": testAccContact_oncallSchedule,
},
"ContactDataSource": {
acctest.CtBasic: testAccContactDataSource_basic,
"tags": testAccSSMContactsContactDataSource_tagsSerial,
acctest.CtBasic: testAccContactDataSource_basic,
"tags": testAccSSMContactsContactDataSource_tagsSerial,
"oncallSchedule": testAccContactDataSource_oncallSchedule,
},
"ContactChannelResource": {
acctest.CtBasic: testAccContactChannel_basic,
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/ssmcontacts_contact.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This data source supports the following arguments:
This data source exports the following attributes in addition to the arguments above:

* `alias` - A unique and identifiable alias of the contact or escalation plan.
* `type` - The type of contact engaged. A single contact is type `PERSONAL` and an escalation plan is type `ESCALATION`.
* `type` - The type of contact engaged. A single contact is type `PERSONAL`, an escalation plan is type `ESCALATION`, and an on-call schedule is type `ONCALL_SCHEDULE`.
* `display_name` - Full friendly name of the contact or escalation plan.
* `rotation_ids` - List of rotation IDs associated with the contact.
* `tags` - Map of tags to assign to the resource.
Loading
Loading