Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/transfer_workflow - new resource #24248

Merged
merged 20 commits into from
Apr 15, 2022
Merged
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/24248.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_transfer_workflow
```

```release-note:enhancement
resource/aws_transfer_server: Add `workflow_details` argument
```
9 changes: 5 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,10 +1943,11 @@ func Provider() *schema.Provider {
"aws_timestreamwrite_database": timestreamwrite.ResourceDatabase(),
"aws_timestreamwrite_table": timestreamwrite.ResourceTable(),

"aws_transfer_access": transfer.ResourceAccess(),
"aws_transfer_server": transfer.ResourceServer(),
"aws_transfer_ssh_key": transfer.ResourceSSHKey(),
"aws_transfer_user": transfer.ResourceUser(),
"aws_transfer_access": transfer.ResourceAccess(),
"aws_transfer_server": transfer.ResourceServer(),
"aws_transfer_ssh_key": transfer.ResourceSSHKey(),
"aws_transfer_user": transfer.ResourceUser(),
"aws_transfer_workflow": transfer.ResourceWorkflow(),

"aws_waf_byte_match_set": waf.ResourceByteMatchSet(),
"aws_waf_geo_match_set": waf.ResourceGeoMatchSet(),
Expand Down
25 changes: 25 additions & 0 deletions internal/service/transfer/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,28 @@ func FindUserByServerIDAndUserName(conn *transfer.Transfer, serverID, userName s

return output.User, nil
}

func FindWorkflowByID(conn *transfer.Transfer, id string) (*transfer.DescribedWorkflow, error) {
input := &transfer.DescribeWorkflowInput{
WorkflowId: aws.String(id),
}

output, err := conn.DescribeWorkflow(input)

if tfawserr.ErrCodeEquals(err, transfer.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.Workflow == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.Workflow, nil
}
136 changes: 133 additions & 3 deletions internal/service/transfer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ func ResourceServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"workflow_details": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"on_upload": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"execution_role": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"workflow_id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -282,6 +309,10 @@ func resourceServerCreate(d *schema.ResourceData, meta interface{}) error {
input.IdentityProviderDetails.Url = aws.String(v.(string))
}

if v, ok := d.GetOk("workflow_details"); ok && len(v.([]interface{})) > 0 {
input.WorkflowDetails = expandWorkflowDetails(v.([]interface{}))
}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}
Expand Down Expand Up @@ -399,6 +430,10 @@ func resourceServerRead(d *schema.ResourceData, meta interface{}) error {
d.Set("url", "")
}

if err := d.Set("workflow_details", flattenWorkflowDetails(output.WorkflowDetails)); err != nil {
return fmt.Errorf("error setting workflow_details: %w", err)
}

tags := KeyValueTags(output.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
Expand Down Expand Up @@ -574,6 +609,10 @@ func resourceServerUpdate(d *schema.ResourceData, meta interface{}) error {
input.SecurityPolicyName = aws.String(d.Get("security_policy_name").(string))
}

if d.HasChange("workflow_details") {
input.WorkflowDetails = expandWorkflowDetails(d.Get("workflow_details").([]interface{}))
}

if offlineUpdate {
if err := stopTransferServer(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return err
Expand Down Expand Up @@ -670,9 +709,13 @@ func resourceServerDelete(d *schema.ResourceData, meta interface{}) error {
}

log.Printf("[DEBUG] Deleting Transfer Server: (%s)", d.Id())
_, err := conn.DeleteServer(&transfer.DeleteServerInput{
ServerId: aws.String(d.Id()),
})
_, err := tfresource.RetryWhenAWSErrMessageContains(1*time.Minute,
func() (interface{}, error) {
return conn.DeleteServer(&transfer.DeleteServerInput{
ServerId: aws.String(d.Id()),
})
},
transfer.ErrCodeInvalidRequestException, "Unable to delete VPC endpoint")

if tfawserr.ErrCodeEquals(err, transfer.ErrCodeResourceNotFoundException) {
return nil
Expand Down Expand Up @@ -753,6 +796,93 @@ func flattenTransferEndpointDetails(apiObject *transfer.EndpointDetails, securit
return tfMap
}

func expandWorkflowDetails(tfMap []interface{}) *transfer.WorkflowDetails {
if tfMap == nil {
return nil
}

tfMapRaw := tfMap[0].(map[string]interface{})

apiObject := &transfer.WorkflowDetails{}

if v, ok := tfMapRaw["on_upload"].([]interface{}); ok && len(v) > 0 {
apiObject.OnUpload = expandWorkflowDetail(v)
}

return apiObject
}

func flattenWorkflowDetails(apiObject *transfer.WorkflowDetails) []interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.OnUpload; v != nil {
tfMap["on_upload"] = flattenWorkflowDetail(v)
}

return []interface{}{tfMap}
}

func expandWorkflowDetail(tfList []interface{}) []*transfer.WorkflowDetail {
if len(tfList) == 0 {
return nil
}

var apiObjects []*transfer.WorkflowDetail

for _, tfMapRaw := range tfList {
tfMap, _ := tfMapRaw.(map[string]interface{})

apiObject := &transfer.WorkflowDetail{}

if v, ok := tfMap["execution_role"].(string); ok && v != "" {
apiObject.ExecutionRole = aws.String(v)
}

if v, ok := tfMap["workflow_id"].(string); ok && v != "" {
apiObject.WorkflowId = aws.String(v)
}

if apiObject == nil {
continue
}

apiObjects = append(apiObjects, apiObject)
}

return apiObjects
}

func flattenWorkflowDetail(apiObjects []*transfer.WorkflowDetail) []interface{} {
if len(apiObjects) == 0 {
return nil
}

var tfList []interface{}

for _, apiObject := range apiObjects {
if apiObject == nil {
continue
}

flattenedObject := map[string]interface{}{}
if v := apiObject.ExecutionRole; v != nil {
flattenedObject["execution_role"] = aws.StringValue(v)
}

if v := apiObject.WorkflowId; v != nil {
flattenedObject["workflow_id"] = aws.StringValue(v)
}

tfList = append(tfList, flattenedObject)
}

return tfList
}

func stopTransferServer(conn *transfer.Transfer, serverID string, timeout time.Duration) error {
input := &transfer.StopServerInput{
ServerId: aws.String(serverID),
Expand Down
Loading