forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_api_gateway_app_attachment.go
131 lines (108 loc) · 3.85 KB
/
resource_alicloud_api_gateway_app_attachment.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cloudapi"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAliyunApigatewayAppAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunApigatewayAppAttachmentCreate,
Read: resourceAliyunApigatewayAppAttachmentRead,
Delete: resourceAliyunApigatewayAppAttachmentDelete,
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"stage_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAllowedStringValue([]string{string(StageNamePre), string(StageNameRelease), string(StageNameTest)}),
},
},
}
}
func resourceAliyunApigatewayAppAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
cloudApiService := CloudApiService{client}
apiId := d.Get("api_id").(string)
groupId := d.Get("group_id").(string)
stageName := d.Get("stage_name").(string)
appId := d.Get("app_id").(string)
authorizationReq := cloudapi.CreateSetAppsAuthoritiesRequest()
authorizationReq.GroupId = groupId
authorizationReq.ApiId = apiId
authorizationReq.AppIds = appId
authorizationReq.StageName = stageName
_, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {
return cloudApiClient.SetAppsAuthorities(authorizationReq)
})
if err != nil {
return fmt.Errorf("APP Attachment: Authorizing api to app got an error: %#v.", err)
}
id := fmt.Sprintf("%s%s%s%s%s%s%s", groupId, COLON_SEPARATED, apiId, COLON_SEPARATED, appId, COLON_SEPARATED, stageName)
err = cloudApiService.WaitForAppAttachmentAuthorization(id, 10)
if err != nil {
return fmt.Errorf("APP Attachment: Authorizing api to app got an error: %#v.", err)
}
d.SetId(id)
return resourceAliyunApigatewayAppAttachmentRead(d, meta)
}
func resourceAliyunApigatewayAppAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
cloudApiService := CloudApiService{client}
_, err := cloudApiService.DescribeAuthorization(d.Id())
if err != nil {
return fmt.Errorf("Describe apigatway App error: %#v", err)
}
split := strings.Split(d.Id(), COLON_SEPARATED)
d.Set("group_id", split[0])
d.Set("api_id", split[1])
d.Set("app_id", split[2])
d.Set("stage_name", split[3])
return nil
}
func resourceAliyunApigatewayAppAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
cloudApiService := CloudApiService{client}
reqRemoveAuth := cloudapi.CreateRemoveAppsAuthoritiesRequest()
split := strings.Split(d.Id(), COLON_SEPARATED)
reqRemoveAuth.GroupId = split[0]
reqRemoveAuth.ApiId = split[1]
reqRemoveAuth.AppIds = split[2]
reqRemoveAuth.StageName = split[3]
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {
return cloudApiClient.RemoveAppsAuthorities(reqRemoveAuth)
})
if err != nil {
if IsExceptedError(err, NotFoundAuthorization) {
return nil
}
return resource.NonRetryableError(fmt.Errorf("Error deleting authorization failed: %#v", err))
}
if _, err := cloudApiService.DescribeAuthorization(d.Id()); err != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(fmt.Errorf("Error describing authorization failed when deleting: %#v", err))
}
return nil
})
}