|
| 1 | +package sfn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/service/sfn" |
| 11 | + "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 16 | + "github.com/hashicorp/terraform-provider-aws/internal/create" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 19 | +) |
| 20 | + |
| 21 | +// @SDKResource("aws_sfn_alias") |
| 22 | +func ResourceAlias() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + CreateWithoutTimeout: resourceAliasCreate, |
| 25 | + ReadWithoutTimeout: resourceAliasRead, |
| 26 | + UpdateWithoutTimeout: resourceAliasUpdate, |
| 27 | + DeleteWithoutTimeout: resourceAliasDelete, |
| 28 | + |
| 29 | + Importer: &schema.ResourceImporter{ |
| 30 | + StateContext: schema.ImportStatePassthroughContext, |
| 31 | + }, |
| 32 | + |
| 33 | + Timeouts: &schema.ResourceTimeout{ |
| 34 | + Create: schema.DefaultTimeout(30 * time.Minute), |
| 35 | + Update: schema.DefaultTimeout(30 * time.Minute), |
| 36 | + Delete: schema.DefaultTimeout(30 * time.Minute), |
| 37 | + }, |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "arn": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "creation_date": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "description": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + }, |
| 51 | + "name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + }, |
| 56 | + "routing_configuration": { |
| 57 | + Type: schema.TypeList, |
| 58 | + Required: true, |
| 59 | + MinItems: 1, |
| 60 | + Elem: &schema.Resource{ |
| 61 | + Schema: map[string]*schema.Schema{ |
| 62 | + "state_machine_version_arn": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + }, |
| 66 | + "weight": { |
| 67 | + Type: schema.TypeInt, |
| 68 | + Required: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +const ( |
| 78 | + ResNameAlias = "Alias" |
| 79 | +) |
| 80 | + |
| 81 | +func resourceAliasCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 82 | + conn := meta.(*conns.AWSClient).SFNConn(ctx) |
| 83 | + |
| 84 | + in := &sfn.CreateStateMachineAliasInput{ |
| 85 | + Name: aws.String(d.Get("name").(string)), |
| 86 | + Description: aws.String(d.Get("description").(string)), |
| 87 | + } |
| 88 | + |
| 89 | + if v, ok := d.GetOk("routing_configuration"); ok && len(v.([]interface{})) > 0 { |
| 90 | + in.RoutingConfiguration = expandAliasRoutingConfiguration(v.([]interface{})) |
| 91 | + } |
| 92 | + |
| 93 | + out, err := conn.CreateStateMachineAliasWithContext(ctx, in) |
| 94 | + if err != nil { |
| 95 | + return create.DiagError(names.SFN, create.ErrActionCreating, ResNameAlias, d.Get("name").(string), err) |
| 96 | + } |
| 97 | + |
| 98 | + if out == nil || out.StateMachineAliasArn == nil { |
| 99 | + return create.DiagError(names.SFN, create.ErrActionCreating, ResNameAlias, d.Get("name").(string), errors.New("empty output")) |
| 100 | + } |
| 101 | + |
| 102 | + d.SetId(aws.StringValue(out.StateMachineAliasArn)) |
| 103 | + |
| 104 | + return resourceAliasRead(ctx, d, meta) |
| 105 | +} |
| 106 | + |
| 107 | +func resourceAliasRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 108 | + conn := meta.(*conns.AWSClient).SFNConn(ctx) |
| 109 | + |
| 110 | + out, err := FindAliasByARN(ctx, conn, d.Id()) |
| 111 | + |
| 112 | + if !d.IsNewResource() && tfresource.NotFound(err) { |
| 113 | + log.Printf("[WARN] SFN Alias (%s) not found, removing from state", d.Id()) |
| 114 | + d.SetId("") |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + return create.DiagError(names.SFN, create.ErrActionReading, ResNameAlias, d.Id(), err) |
| 120 | + } |
| 121 | + |
| 122 | + d.Set("arn", out.StateMachineAliasArn) |
| 123 | + d.Set("name", out.Name) |
| 124 | + d.Set("description", out.Description) |
| 125 | + d.Set("creation_date", aws.TimeValue(out.CreationDate).Format(time.RFC3339)) |
| 126 | + d.SetId(aws.StringValue(out.StateMachineAliasArn)) |
| 127 | + |
| 128 | + if err := d.Set("routing_configuration", flattenAliasRoutingConfiguration(out.RoutingConfiguration)); err != nil { |
| 129 | + return create.DiagError(names.SFN, create.ErrActionSetting, ResNameAlias, d.Id(), err) |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func resourceAliasUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 135 | + conn := meta.(*conns.AWSClient).SFNConn(ctx) |
| 136 | + |
| 137 | + update := false |
| 138 | + |
| 139 | + in := &sfn.UpdateStateMachineAliasInput{ |
| 140 | + StateMachineAliasArn: aws.String(d.Id()), |
| 141 | + } |
| 142 | + |
| 143 | + if d.HasChanges("description") { |
| 144 | + in.Description = aws.String(d.Get("description").(string)) |
| 145 | + update = true |
| 146 | + } |
| 147 | + |
| 148 | + if d.HasChange("routing_configuration") { |
| 149 | + in.RoutingConfiguration = expandAliasRoutingConfiguration(d.Get("routing_configuration").([]interface{})) |
| 150 | + update = true |
| 151 | + } |
| 152 | + |
| 153 | + if !update { |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + log.Printf("[DEBUG] Updating SFN Alias (%s): %#v", d.Id(), in) |
| 158 | + _, err := conn.UpdateStateMachineAliasWithContext(ctx, in) |
| 159 | + if err != nil { |
| 160 | + return create.DiagError(names.SFN, create.ErrActionUpdating, ResNameAlias, d.Id(), err) |
| 161 | + } |
| 162 | + |
| 163 | + return resourceAliasRead(ctx, d, meta) |
| 164 | +} |
| 165 | + |
| 166 | +func resourceAliasDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 167 | + conn := meta.(*conns.AWSClient).SFNConn(ctx) |
| 168 | + log.Printf("[INFO] Deleting SFN Alias %s", d.Id()) |
| 169 | + |
| 170 | + _, err := conn.DeleteStateMachineAliasWithContext(ctx, &sfn.DeleteStateMachineAliasInput{ |
| 171 | + StateMachineAliasArn: aws.String(d.Id()), |
| 172 | + }) |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + return create.DiagError(names.SFN, create.ErrActionDeleting, ResNameAlias, d.Id(), err) |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +func FindAliasByARN(ctx context.Context, conn *sfn.SFN, arn string) (*sfn.DescribeStateMachineAliasOutput, error) { |
| 182 | + in := &sfn.DescribeStateMachineAliasInput{ |
| 183 | + StateMachineAliasArn: aws.String(arn), |
| 184 | + } |
| 185 | + out, err := conn.DescribeStateMachineAliasWithContext(ctx, in) |
| 186 | + if tfawserr.ErrCodeEquals(err, sfn.ErrCodeResourceNotFound) { |
| 187 | + return nil, &retry.NotFoundError{ |
| 188 | + LastError: err, |
| 189 | + LastRequest: in, |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + if err != nil { |
| 194 | + return nil, err |
| 195 | + } |
| 196 | + |
| 197 | + if out == nil { |
| 198 | + return nil, tfresource.NewEmptyResultError(in) |
| 199 | + } |
| 200 | + |
| 201 | + return out, nil |
| 202 | +} |
| 203 | + |
| 204 | +func flattenAliasRoutingConfigurationItem(apiObject *sfn.RoutingConfigurationListItem) map[string]interface{} { |
| 205 | + if apiObject == nil { |
| 206 | + return nil |
| 207 | + } |
| 208 | + |
| 209 | + tfMap := map[string]interface{}{} |
| 210 | + |
| 211 | + if v := apiObject.StateMachineVersionArn; v != nil { |
| 212 | + tfMap["state_machine_version_arn"] = aws.StringValue(v) |
| 213 | + } |
| 214 | + |
| 215 | + if v := apiObject.Weight; v != nil { |
| 216 | + tfMap["weight"] = aws.Int64Value(v) |
| 217 | + } |
| 218 | + |
| 219 | + return tfMap |
| 220 | +} |
| 221 | + |
| 222 | +func flattenAliasRoutingConfiguration(apiObjects []*sfn.RoutingConfigurationListItem) []interface{} { |
| 223 | + if len(apiObjects) == 0 { |
| 224 | + return nil |
| 225 | + } |
| 226 | + |
| 227 | + var tfList []interface{} |
| 228 | + |
| 229 | + for _, apiObject := range apiObjects { |
| 230 | + if apiObject == nil { |
| 231 | + continue |
| 232 | + } |
| 233 | + |
| 234 | + tfList = append(tfList, flattenAliasRoutingConfigurationItem(apiObject)) |
| 235 | + } |
| 236 | + |
| 237 | + return tfList |
| 238 | +} |
| 239 | + |
| 240 | +func expandAliasRoutingConfiguration(tfList []interface{}) []*sfn.RoutingConfigurationListItem { |
| 241 | + if len(tfList) == 0 { |
| 242 | + return nil |
| 243 | + } |
| 244 | + var configurationListItems []*sfn.RoutingConfigurationListItem |
| 245 | + |
| 246 | + for _, tfMapRaw := range tfList { |
| 247 | + tfMap, ok := tfMapRaw.(map[string]interface{}) |
| 248 | + |
| 249 | + if !ok { |
| 250 | + continue |
| 251 | + } |
| 252 | + |
| 253 | + configurationListItem := expandAliasRoutingConfigurationItem(tfMap) |
| 254 | + |
| 255 | + if configurationListItem == nil { |
| 256 | + continue |
| 257 | + } |
| 258 | + |
| 259 | + configurationListItems = append(configurationListItems, configurationListItem) |
| 260 | + } |
| 261 | + |
| 262 | + return configurationListItems |
| 263 | +} |
| 264 | + |
| 265 | +func expandAliasRoutingConfigurationItem(tfMap map[string]interface{}) *sfn.RoutingConfigurationListItem { |
| 266 | + if tfMap == nil { |
| 267 | + return nil |
| 268 | + } |
| 269 | + |
| 270 | + apiObject := &sfn.RoutingConfigurationListItem{} |
| 271 | + if v, ok := tfMap["state_machine_version_arn"].(string); ok && v != "" { |
| 272 | + apiObject.StateMachineVersionArn = aws.String(v) |
| 273 | + } |
| 274 | + |
| 275 | + if v, ok := tfMap["weight"].(int); ok && v != 0 { |
| 276 | + apiObject.Weight = aws.Int64(int64(v)) |
| 277 | + } |
| 278 | + |
| 279 | + return apiObject |
| 280 | +} |
0 commit comments