Skip to content

Commit

Permalink
data-source/alicloud_api_gateway_apis: Fixed the read bug; Added the …
Browse files Browse the repository at this point in the history
…field api_id
  • Loading branch information
MrWolong authored and ChenHanZhang committed May 29, 2024
1 parent 0ba82a1 commit b2a267d
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 181 deletions.
174 changes: 92 additions & 82 deletions alicloud/data_source_alicloud_api_gateway_apis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alicloud

import (
"fmt"
"regexp"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
Expand All @@ -10,39 +11,37 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceAlicloudApiGatewayApis() *schema.Resource {
func dataSourceAliCloudApiGatewayApis() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudApigatewayApisRead,

Read: dataSourceAliCloudApiGatewayApisRead,
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"api_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Field 'api_id' has been deprecated from provider version 1.52.2. New field 'ids' replaces it.",
},
"ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.ValidateRegexp,
},
"group_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"api_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
// Computed values
"names": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -57,23 +56,27 @@ func dataSourceAlicloudApiGatewayApis() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"name": {
"group_id": {
Type: schema.TypeString,
Computed: true,
},
"region_id": {
"api_id": {
Type: schema.TypeString,
Computed: true,
},
"group_id": {
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
"region_id": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -83,109 +86,116 @@ func dataSourceAlicloudApiGatewayApis() *schema.Resource {
},
}
}
func dataSourceAlicloudApigatewayApisRead(d *schema.ResourceData, meta interface{}) error {

func dataSourceAliCloudApiGatewayApisRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

request := cloudapi.CreateDescribeApisRequest()
request.RegionId = client.RegionId
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)

if groupId, ok := d.GetOk("group_id"); ok {
request.GroupId = groupId.(string)
if v, ok := d.GetOk("group_id"); ok {
request.GroupId = v.(string)
}
if apiId, ok := d.GetOk("api_id"); ok {
request.ApiId = apiId.(string)

if v, ok := d.GetOk("api_id"); ok {
request.ApiId = v.(string)
}

request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)
var objects []cloudapi.ApiSummary
idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
if vv == nil {
continue
}
idsMap[vv.(string)] = vv.(string)
}
}

var allapis []cloudapi.ApiSummary
var apiNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
if err != nil {
return WrapError(err)
}
apiNameRegex = r
}

for {
raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {
response, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {
return cloudApiClient.DescribeApis(request)
})
addDebug(request.GetActionName(), response, request.RpcRequest, request)

if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_api_gateway_apis", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*cloudapi.DescribeApisResponse)

allapis = append(allapis, response.ApiSummarys.ApiSummary...)
result := response.(*cloudapi.DescribeApisResponse).ApiSummarys.ApiSummary
for _, item := range result {
if len(idsMap) > 0 {
if _, ok := idsMap[fmt.Sprintf("%v:%v", item.GroupId, item.ApiId)]; !ok {
continue
}
}

if apiNameRegex != nil && !apiNameRegex.MatchString(fmt.Sprint(item.ApiName)) {
continue
}

if len(allapis) < PageSizeLarge {
objects = append(objects, item)
}

if len(result) < PageSizeLarge {
break
}

page, err := getNextpageNumber(request.PageNumber)
pageNum, err := getNextpageNumber(request.PageNumber)
if err != nil {
return WrapError(err)
}
request.PageNumber = page
}

var filteredApisTemp []cloudapi.ApiSummary

// ids
idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
if vv == nil {
continue
}
idsMap[vv.(string)] = vv.(string)
}
}
for _, api := range allapis {
if v, ok := d.GetOk("name_regex"); ok && v.(string) != "" {
r := regexp.MustCompile(v.(string))
if !r.MatchString(api.ApiName) {
continue
}
}
if len(idsMap) > 0 {
if _, ok := idsMap[api.ApiId]; !ok {
continue
}
}
filteredApisTemp = append(filteredApisTemp, api)
request.PageNumber = pageNum
}

return apiGatewayApisDescribeSummarys(d, filteredApisTemp, meta)
}

func apiGatewayApisDescribeSummarys(d *schema.ResourceData, apis []cloudapi.ApiSummary, meta interface{}) error {
var ids []string
var names []string
var s []map[string]interface{}
for _, api := range apis {
ids := make([]string, 0)
names := make([]interface{}, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
mapping := map[string]interface{}{
"id": api.ApiId,
"name": api.ApiName,
"region_id": api.RegionId,
"group_id": api.GroupId,
"group_name": api.GroupName,
"description": api.Description,
"id": fmt.Sprintf("%v:%v", object.GroupId, object.ApiId),
"group_id": fmt.Sprint(object.GroupId),
"api_id": fmt.Sprint(object.ApiId),
"name": object.ApiName,
"description": object.Description,
"group_name": object.GroupName,
"region_id": object.RegionId,
}
ids = append(ids, api.ApiId)
s = append(s, mapping)
names = append(names, api.ApiName)

ids = append(ids, fmt.Sprint(mapping["id"]))
names = append(names, object.ApiName)
s = append(s, mapping)
}

d.SetId(dataResourceIdHash(ids))
if err := d.Set("apis", s); err != nil {
return WrapError(err)
}

if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}

if err := d.Set("names", names); err != nil {
return WrapError(err)
}

// create a json file in current directory and write data source to it.
if err := d.Set("apis", s); err != nil {
return WrapError(err)
}

if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}

return nil
}
Loading

0 comments on commit b2a267d

Please sign in to comment.