Skip to content

Commit 6f8c69f

Browse files
authored
Merge pull request #24821 from kamilturek/f-aws-location-place-index
r/aws_location_place_index - new resource
2 parents bb564d1 + 563e191 commit 6f8c69f

File tree

5 files changed

+615
-1
lines changed

5 files changed

+615
-1
lines changed

.changelog/24821.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
aws_location_place_index
3+
```

internal/provider/provider.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ func Provider() *schema.Provider {
16261626
"aws_lightsail_static_ip": lightsail.ResourceStaticIP(),
16271627
"aws_lightsail_static_ip_attachment": lightsail.ResourceStaticIPAttachment(),
16281628

1629-
"aws_location_map": location.ResourceMap(),
1629+
"aws_location_map": location.ResourceMap(),
1630+
"aws_location_place_index": location.ResourcePlaceIndex(),
16301631

16311632
"aws_macie_member_account_association": macie.ResourceMemberAccountAssociation(),
16321633
"aws_macie_s3_bucket_association": macie.ResourceS3BucketAssociation(),
+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package location
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/service/locationservice"
10+
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
"github.com/hashicorp/terraform-provider-aws/internal/conns"
14+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
15+
"github.com/hashicorp/terraform-provider-aws/internal/verify"
16+
)
17+
18+
func ResourcePlaceIndex() *schema.Resource {
19+
return &schema.Resource{
20+
Create: resourcePlaceIndexCreate,
21+
Read: resourcePlaceIndexRead,
22+
Update: resourcePlaceIndexUpdate,
23+
Delete: resourcePlaceIndexDelete,
24+
Importer: &schema.ResourceImporter{
25+
State: schema.ImportStatePassthrough,
26+
},
27+
Schema: map[string]*schema.Schema{
28+
"create_time": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"data_source": {
33+
Type: schema.TypeString,
34+
Required: true,
35+
ForceNew: true,
36+
},
37+
"data_source_configuration": {
38+
Type: schema.TypeList,
39+
Optional: true,
40+
Computed: true,
41+
MaxItems: 1,
42+
Elem: &schema.Resource{
43+
Schema: map[string]*schema.Schema{
44+
"intended_use": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
Default: locationservice.IntendedUseSingleUse,
48+
ValidateFunc: validation.StringInSlice(locationservice.IntendedUse_Values(), false),
49+
},
50+
},
51+
},
52+
},
53+
"description": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
ValidateFunc: validation.StringLenBetween(0, 1000),
57+
},
58+
"index_arn": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"index_name": {
63+
Type: schema.TypeString,
64+
Required: true,
65+
ForceNew: true,
66+
ValidateFunc: validation.StringLenBetween(1, 100),
67+
},
68+
"update_time": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"tags": tftags.TagsSchema(),
73+
"tags_all": tftags.TagsSchemaComputed(),
74+
},
75+
CustomizeDiff: verify.SetTagsDiff,
76+
}
77+
}
78+
79+
func resourcePlaceIndexCreate(d *schema.ResourceData, meta interface{}) error {
80+
conn := meta.(*conns.AWSClient).LocationConn
81+
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
82+
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
83+
84+
input := &locationservice.CreatePlaceIndexInput{}
85+
86+
if v, ok := d.GetOk("data_source"); ok {
87+
input.DataSource = aws.String(v.(string))
88+
}
89+
90+
if v, ok := d.GetOk("data_source_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
91+
input.DataSourceConfiguration = expandDataSourceConfiguration(v.([]interface{})[0].(map[string]interface{}))
92+
}
93+
94+
if v, ok := d.GetOk("description"); ok {
95+
input.Description = aws.String(v.(string))
96+
}
97+
98+
if v, ok := d.GetOk("index_name"); ok {
99+
input.IndexName = aws.String(v.(string))
100+
}
101+
102+
if len(tags) > 0 {
103+
input.Tags = Tags(tags.IgnoreAWS())
104+
}
105+
106+
output, err := conn.CreatePlaceIndex(input)
107+
108+
if err != nil {
109+
return fmt.Errorf("error creating place index: %w", err)
110+
}
111+
112+
if output == nil {
113+
return fmt.Errorf("error creating place index: empty result")
114+
}
115+
116+
d.SetId(aws.StringValue(output.IndexName))
117+
118+
return resourcePlaceIndexRead(d, meta)
119+
}
120+
121+
func resourcePlaceIndexRead(d *schema.ResourceData, meta interface{}) error {
122+
conn := meta.(*conns.AWSClient).LocationConn
123+
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
124+
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
125+
126+
input := &locationservice.DescribePlaceIndexInput{
127+
IndexName: aws.String(d.Id()),
128+
}
129+
130+
output, err := conn.DescribePlaceIndex(input)
131+
132+
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) {
133+
log.Printf("[WARN] Location Service Place Index (%s) not found, removing from state", d.Id())
134+
d.SetId("")
135+
return nil
136+
}
137+
138+
if err != nil {
139+
return fmt.Errorf("error getting Location Service Place Index (%s): %w", d.Id(), err)
140+
}
141+
142+
if output == nil {
143+
return fmt.Errorf("error getting Location Service Place Index (%s): empty response", d.Id())
144+
}
145+
146+
d.Set("create_time", aws.TimeValue(output.CreateTime).Format(time.RFC3339))
147+
d.Set("data_source", output.DataSource)
148+
149+
if output.DataSourceConfiguration != nil {
150+
d.Set("data_source_configuration", []interface{}{flattenDataSourceConfiguration(output.DataSourceConfiguration)})
151+
} else {
152+
d.Set("data_source_configuration", nil)
153+
}
154+
155+
d.Set("description", output.Description)
156+
d.Set("index_arn", output.IndexArn)
157+
d.Set("index_name", output.IndexName)
158+
159+
tags := KeyValueTags(output.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
160+
161+
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
162+
return fmt.Errorf("error setting tags: %w", err)
163+
}
164+
165+
if err := d.Set("tags_all", tags.Map()); err != nil {
166+
return fmt.Errorf("error setting tags_all: %w", err)
167+
}
168+
169+
d.Set("update_time", aws.TimeValue(output.UpdateTime).Format(time.RFC3339))
170+
171+
return nil
172+
}
173+
174+
func resourcePlaceIndexUpdate(d *schema.ResourceData, meta interface{}) error {
175+
conn := meta.(*conns.AWSClient).LocationConn
176+
177+
if d.HasChanges("data_source_configuration", "description") {
178+
input := &locationservice.UpdatePlaceIndexInput{
179+
IndexName: aws.String(d.Id()),
180+
// Deprecated but still required by the API
181+
PricingPlan: aws.String(locationservice.PricingPlanRequestBasedUsage),
182+
}
183+
184+
if v, ok := d.GetOk("data_source_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
185+
input.DataSourceConfiguration = expandDataSourceConfiguration(v.([]interface{})[0].(map[string]interface{}))
186+
}
187+
188+
if v, ok := d.GetOk("description"); ok {
189+
input.Description = aws.String(v.(string))
190+
}
191+
192+
_, err := conn.UpdatePlaceIndex(input)
193+
194+
if err != nil {
195+
return fmt.Errorf("error updating Location Service Place Index (%s): %w", d.Id(), err)
196+
}
197+
}
198+
199+
if d.HasChange("tags_all") {
200+
o, n := d.GetChange("tags_all")
201+
202+
if err := UpdateTags(conn, d.Get("index_arn").(string), o, n); err != nil {
203+
return fmt.Errorf("error updating tags for Location Service Place Index (%s): %w", d.Id(), err)
204+
}
205+
}
206+
207+
return resourcePlaceIndexRead(d, meta)
208+
}
209+
210+
func resourcePlaceIndexDelete(d *schema.ResourceData, meta interface{}) error {
211+
conn := meta.(*conns.AWSClient).LocationConn
212+
213+
input := &locationservice.DeletePlaceIndexInput{
214+
IndexName: aws.String(d.Id()),
215+
}
216+
217+
_, err := conn.DeletePlaceIndex(input)
218+
219+
if tfawserr.ErrCodeEquals(err, locationservice.ErrCodeResourceNotFoundException) {
220+
return nil
221+
}
222+
223+
if err != nil {
224+
return fmt.Errorf("error deleting Location Service Place Index (%s): %w", d.Id(), err)
225+
}
226+
227+
return nil
228+
}
229+
230+
func expandDataSourceConfiguration(tfMap map[string]interface{}) *locationservice.DataSourceConfiguration {
231+
if tfMap == nil {
232+
return nil
233+
}
234+
235+
apiObject := &locationservice.DataSourceConfiguration{}
236+
237+
if v, ok := tfMap["intended_use"].(string); ok && v != "" {
238+
apiObject.IntendedUse = aws.String(v)
239+
}
240+
241+
return apiObject
242+
}
243+
244+
func flattenDataSourceConfiguration(apiObject *locationservice.DataSourceConfiguration) map[string]interface{} {
245+
if apiObject == nil {
246+
return nil
247+
}
248+
249+
tfMap := map[string]interface{}{}
250+
251+
if v := apiObject.IntendedUse; v != nil {
252+
tfMap["intended_use"] = aws.StringValue(v)
253+
}
254+
255+
return tfMap
256+
}

0 commit comments

Comments
 (0)