diff --git a/.changelog/24682.txt b/.changelog/24682.txt new file mode 100644 index 00000000000..af66266da7d --- /dev/null +++ b/.changelog/24682.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_location_map +``` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 842c03c7618..f5ba97c54c1 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -111,6 +111,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/service/lexmodels" "github.com/hashicorp/terraform-provider-aws/internal/service/licensemanager" "github.com/hashicorp/terraform-provider-aws/internal/service/lightsail" + "github.com/hashicorp/terraform-provider-aws/internal/service/location" "github.com/hashicorp/terraform-provider-aws/internal/service/logs" "github.com/hashicorp/terraform-provider-aws/internal/service/macie" "github.com/hashicorp/terraform-provider-aws/internal/service/macie2" @@ -1607,6 +1608,8 @@ func Provider() *schema.Provider { "aws_lightsail_static_ip": lightsail.ResourceStaticIP(), "aws_lightsail_static_ip_attachment": lightsail.ResourceStaticIPAttachment(), + "aws_location_map": location.ResourceMap(), + "aws_macie_member_account_association": macie.ResourceMemberAccountAssociation(), "aws_macie_s3_bucket_association": macie.ResourceS3BucketAssociation(), diff --git a/internal/service/location/generate.go b/internal/service/location/generate.go new file mode 100644 index 00000000000..86ff1b32d8c --- /dev/null +++ b/internal/service/location/generate.go @@ -0,0 +1,4 @@ +//go:generate go run ../../generate/tags/main.go -ServiceTagsMap -UpdateTags +// ONLY generate directives and package declaration! Do not add anything else to this file. + +package location diff --git a/internal/service/location/map.go b/internal/service/location/map.go new file mode 100644 index 00000000000..fe389dbbb7b --- /dev/null +++ b/internal/service/location/map.go @@ -0,0 +1,238 @@ +package location + +import ( + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/locationservice" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" + "github.com/hashicorp/terraform-provider-aws/internal/verify" +) + +func ResourceMap() *schema.Resource { + return &schema.Resource{ + Create: resourceMapCreate, + Read: resourceMapRead, + Update: resourceMapUpdate, + Delete: resourceMapDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "configuration": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "style": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 100), + }, + }, + }, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringLenBetween(0, 1000), + }, + "map_arn": { + Type: schema.TypeString, + Computed: true, + }, + "map_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 100), + }, + "update_time": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tftags.TagsSchema(), + "tags_all": tftags.TagsSchemaComputed(), + }, + CustomizeDiff: verify.SetTagsDiff, + } +} + +func resourceMapCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).LocationConn + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{}))) + + input := &locationservice.CreateMapInput{} + + if v, ok := d.GetOk("configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { + input.Configuration = expandConfiguration(v.([]interface{})[0].(map[string]interface{})) + } + + if v, ok := d.GetOk("description"); ok { + input.Description = aws.String(v.(string)) + } + + if v, ok := d.GetOk("map_name"); ok { + input.MapName = aws.String(v.(string)) + } + + if len(tags) > 0 { + input.Tags = Tags(tags.IgnoreAWS()) + } + + output, err := conn.CreateMap(input) + + if err != nil { + return fmt.Errorf("error creating map: %w", err) + } + + if output == nil { + return fmt.Errorf("error creating map: empty result") + } + + d.SetId(aws.StringValue(output.MapName)) + + return resourceMapRead(d, meta) +} + +func resourceMapRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).LocationConn + defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig + ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig + + input := &locationservice.DescribeMapInput{ + MapName: aws.String(d.Id()), + } + + output, err := conn.DescribeMap(input) + + if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) { + log.Printf("[WARN] Location Service Map (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + if err != nil { + return fmt.Errorf("error getting Location Service Map (%s): %w", d.Id(), err) + } + + if output == nil { + return fmt.Errorf("error getting Location Service Map (%s): empty response", d.Id()) + } + + if output.Configuration != nil { + d.Set("configuration", []interface{}{flattenConfiguration(output.Configuration)}) + } else { + d.Set("configuration", nil) + } + + d.Set("create_time", aws.TimeValue(output.CreateTime).Format(time.RFC3339)) + d.Set("description", output.Description) + d.Set("map_arn", output.MapArn) + d.Set("map_name", output.MapName) + d.Set("update_time", aws.TimeValue(output.UpdateTime).Format(time.RFC3339)) + + tags := KeyValueTags(output.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig) + + if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { + return fmt.Errorf("error setting tags: %w", err) + } + + if err := d.Set("tags_all", tags.Map()); err != nil { + return fmt.Errorf("error setting tags_all: %w", err) + } + + return nil +} + +func resourceMapUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).LocationConn + + if d.HasChange("description") { + input := &locationservice.UpdateMapInput{ + MapName: aws.String(d.Id()), + } + + if v, ok := d.GetOk("description"); ok { + input.Description = aws.String(v.(string)) + } + + _, err := conn.UpdateMap(input) + + if err != nil { + return fmt.Errorf("error updating Location Service Map (%s): %w", d.Id(), err) + } + } + + if d.HasChange("tags_all") { + o, n := d.GetChange("tags_all") + + if err := UpdateTags(conn, d.Get("map_arn").(string), o, n); err != nil { + return fmt.Errorf("error updating tags for Location Service Map (%s): %w", d.Id(), err) + } + } + + return resourceMapRead(d, meta) +} + +func resourceMapDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*conns.AWSClient).LocationConn + + input := &locationservice.DeleteMapInput{ + MapName: aws.String(d.Id()), + } + + _, err := conn.DeleteMap(input) + + if tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) { + return nil + } + + if err != nil { + return fmt.Errorf("error deleting Location Service Map (%s): %w", d.Id(), err) + } + + return nil +} + +func expandConfiguration(tfMap map[string]interface{}) *locationservice.MapConfiguration { + if tfMap == nil { + return nil + } + + apiObject := &locationservice.MapConfiguration{} + + if v, ok := tfMap["style"].(string); ok && v != "" { + apiObject.Style = aws.String(v) + } + + return apiObject +} + +func flattenConfiguration(apiObject *locationservice.MapConfiguration) map[string]interface{} { + if apiObject == nil { + return nil + } + + tfMap := map[string]interface{}{} + + if v := apiObject.Style; v != nil { + tfMap["style"] = aws.StringValue(v) + } + + return tfMap +} diff --git a/internal/service/location/map_test.go b/internal/service/location/map_test.go new file mode 100644 index 00000000000..a0b76d1270b --- /dev/null +++ b/internal/service/location/map_test.go @@ -0,0 +1,260 @@ +package location_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/locationservice" + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + tflocation "github.com/hashicorp/terraform-provider-aws/internal/service/location" +) + +func TestAccLocationMap_basic(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_location_map.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, locationservice.EndpointsID), + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccCheckMapDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigMap_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "configuration.#", "1"), + resource.TestCheckResourceAttr(resourceName, "configuration.0.style", "VectorHereBerlin"), + acctest.CheckResourceAttrRFC3339(resourceName, "create_time"), + resource.TestCheckResourceAttr(resourceName, "description", ""), + acctest.CheckResourceAttrRegionalARN(resourceName, "map_arn", "geo", fmt.Sprintf("map/%s", rName)), + resource.TestCheckResourceAttr(resourceName, "map_name", rName), + acctest.CheckResourceAttrRFC3339(resourceName, "update_time"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccLocationMap_disappears(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_location_map.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, locationservice.EndpointsID), + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccCheckMapDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigMap_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + acctest.CheckResourceDisappears(acctest.Provider, tflocation.ResourceMap(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccLocationMap_description(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_location_map.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, locationservice.EndpointsID), + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccCheckMapDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigMap_description(rName, "description1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "description1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccConfigMap_description(rName, "description2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "description2"), + ), + }, + }, + }) +} + +func TestAccLocationMap_tags(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_location_map.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, locationservice.EndpointsID), + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccCheckMapDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigMap_tags1(rName, "key1", "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccConfigMap_tags2(rName, "key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccConfigMap_tags1(rName, "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckMapExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + }, + }) +} + +func testAccCheckMapDestroy(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).LocationConn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_location_map" { + continue + } + + input := &locationservice.DescribeMapInput{ + MapName: aws.String(rs.Primary.ID), + } + + output, err := conn.DescribeMap(input) + + if tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) { + continue + } + + if err != nil { + return fmt.Errorf("error getting Location Service Map (%s): %w", rs.Primary.ID, err) + } + + if output != nil { + return fmt.Errorf("Location Service Map (%s) still exists", rs.Primary.ID) + } + } + + return nil +} + +func testAccCheckMapExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + + if !ok { + return fmt.Errorf("resource not found: %s", resourceName) + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).LocationConn + + input := &locationservice.DescribeMapInput{ + MapName: aws.String(rs.Primary.ID), + } + + _, err := conn.DescribeMap(input) + + if err != nil { + return fmt.Errorf("error getting Location Service Map (%s): %w", rs.Primary.ID, err) + } + + return nil + } +} + +func testAccConfigMap_basic(rName string) string { + return fmt.Sprintf(` +resource "aws_location_map" "test" { + configuration { + style = "VectorHereBerlin" + } + + map_name = %[1]q +} +`, rName) +} + +func testAccConfigMap_description(rName, description string) string { + return fmt.Sprintf(` +resource "aws_location_map" "test" { + configuration { + style = "VectorHereBerlin" + } + + map_name = %[1]q + description = %[2]q +} +`, rName, description) +} + +func testAccConfigMap_tags1(rName, tagKey1, tagValue1 string) string { + return fmt.Sprintf(` +resource "aws_location_map" "test" { + configuration { + style = "VectorHereBerlin" + } + + map_name = %[1]q + + tags = { + %[2]q = %[3]q + } +} +`, rName, tagKey1, tagValue1) +} + +func testAccConfigMap_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` +resource "aws_location_map" "test" { + configuration { + style = "VectorHereBerlin" + } + + map_name = %[1]q + + tags = { + %[2]q = %[3]q + %[4]q = %[5]q + } +} +`, rName, tagKey1, tagValue1, tagKey2, tagValue2) +} diff --git a/internal/service/location/tags_gen.go b/internal/service/location/tags_gen.go new file mode 100644 index 00000000000..f870f844d21 --- /dev/null +++ b/internal/service/location/tags_gen.go @@ -0,0 +1,58 @@ +// Code generated by internal/generate/tags/main.go; DO NOT EDIT. +package location + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/locationservice" + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" +) + +// map[string]*string handling + +// Tags returns location service tags. +func Tags(tags tftags.KeyValueTags) map[string]*string { + return aws.StringMap(tags.Map()) +} + +// KeyValueTags creates KeyValueTags from location service tags. +func KeyValueTags(tags map[string]*string) tftags.KeyValueTags { + return tftags.New(tags) +} + +// UpdateTags updates location service tags. +// The identifier is typically the Amazon Resource Name (ARN), although +// it may also be a different identifier depending on the service. +func UpdateTags(conn *locationservice.LocationService, identifier string, oldTagsMap interface{}, newTagsMap interface{}) error { + oldTags := tftags.New(oldTagsMap) + newTags := tftags.New(newTagsMap) + + if removedTags := oldTags.Removed(newTags); len(removedTags) > 0 { + input := &locationservice.UntagResourceInput{ + ResourceArn: aws.String(identifier), + TagKeys: aws.StringSlice(removedTags.IgnoreAWS().Keys()), + } + + _, err := conn.UntagResource(input) + + if err != nil { + return fmt.Errorf("error untagging resource (%s): %w", identifier, err) + } + } + + if updatedTags := oldTags.Updated(newTags); len(updatedTags) > 0 { + input := &locationservice.TagResourceInput{ + ResourceArn: aws.String(identifier), + Tags: Tags(updatedTags.IgnoreAWS()), + } + + _, err := conn.TagResource(input) + + if err != nil { + return fmt.Errorf("error tagging resource (%s): %w", identifier, err) + } + } + + return nil +} diff --git a/website/docs/r/location_map.html.markdown b/website/docs/r/location_map.html.markdown new file mode 100644 index 00000000000..35a4d150aa7 --- /dev/null +++ b/website/docs/r/location_map.html.markdown @@ -0,0 +1,58 @@ +--- +subcategory: "Location" +layout: "aws" +page_title: "AWS: aws_location_map" +description: |- + Provides a Location Service Map. +--- + +# Resource: aws_location_map + +Provides a Location Service Map. + +## Example Usage + +```terraform +resource "aws_location_map" "example" { + configuration { + style = "VectorHereBerlin" + } + + map_name = "example" +} +``` + +## Argument Reference + +The following arguments are required: + +* `configuration` - (Required) Configuration block with the map style selected from an available data provider. Detailed below. +* `map_name` - (Required) The name for the map resource. + +The following arguments are optional: + +* `description` - (Optional) An optional description for the map resource. +* `tags` - (Optional) Key-value tags for the map. If configured with a provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `map_arn` - The Amazon Resource Name (ARN) for the map resource. Used to specify a resource across all AWS. +* `create_time` - The timestamp for when the map resource was created in ISO 8601 format. +* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block). +* `update_time` - The timestamp for when the map resource was last update in ISO 8601. + +## configuration + +The following arguments are required: + +* `style` - (Required) Specifies the map style selected from an available data provider. Valid values can be found in the [Location Service CreateMap API Reference](https://docs.aws.amazon.com/location-maps/latest/APIReference/API_CreateMap.html). + +## Import + +`aws_location_map` resources can be imported using the map name, e.g.: + +``` +$ terraform import aws_location_map.example example +```