-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathdata_set.go
157 lines (130 loc) · 4.84 KB
/
data_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dataexchange
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dataexchange"
awstypes "github.com/aws/aws-sdk-go-v2/service/dataexchange/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
// @SDKResource("aws_dataexchange_data_set", name="Data Set")
// @Tags(identifierAttribute="arn")
func ResourceDataSet() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceDataSetCreate,
ReadWithoutTimeout: resourceDataSetRead,
UpdateWithoutTimeout: resourceDataSetUpdate,
DeleteWithoutTimeout: resourceDataSetDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
names.AttrARN: {
Type: schema.TypeString,
Computed: true,
},
"asset_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[awstypes.AssetType](),
},
names.AttrDescription: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 16348),
},
names.AttrName: {
Type: schema.TypeString,
Required: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
CustomizeDiff: verify.SetTagsDiff,
}
}
func resourceDataSetCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DataExchangeClient(ctx)
input := &dataexchange.CreateDataSetInput{
Name: aws.String(d.Get(names.AttrName).(string)),
AssetType: awstypes.AssetType(d.Get("asset_type").(string)),
Description: aws.String(d.Get(names.AttrDescription).(string)),
Tags: getTagsIn(ctx),
}
out, err := conn.CreateDataSet(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating DataExchange DataSet: %s", err)
}
d.SetId(aws.ToString(out.Id))
return append(diags, resourceDataSetRead(ctx, d, meta)...)
}
func resourceDataSetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DataExchangeClient(ctx)
dataSet, err := FindDataSetById(ctx, conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] DataExchange DataSet (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading DataExchange DataSet (%s): %s", d.Id(), err)
}
d.Set("asset_type", dataSet.AssetType)
d.Set(names.AttrName, dataSet.Name)
d.Set(names.AttrDescription, dataSet.Description)
d.Set(names.AttrARN, dataSet.Arn)
setTagsOut(ctx, dataSet.Tags)
return diags
}
func resourceDataSetUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DataExchangeClient(ctx)
if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
input := &dataexchange.UpdateDataSetInput{
DataSetId: aws.String(d.Id()),
}
if d.HasChange(names.AttrName) {
input.Name = aws.String(d.Get(names.AttrName).(string))
}
if d.HasChange(names.AttrDescription) {
input.Description = aws.String(d.Get(names.AttrDescription).(string))
}
log.Printf("[DEBUG] Updating DataExchange DataSet: %s", d.Id())
_, err := conn.UpdateDataSet(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating DataExchange DataSet (%s): %s", d.Id(), err)
}
}
return append(diags, resourceDataSetRead(ctx, d, meta)...)
}
func resourceDataSetDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DataExchangeClient(ctx)
input := &dataexchange.DeleteDataSetInput{
DataSetId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting DataExchange DataSet: %s", d.Id())
_, err := conn.DeleteDataSet(ctx, input)
if err != nil {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}
return sdkdiag.AppendErrorf(diags, "deleting DataExchange DataSet: %s", err)
}
return diags
}