github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/subnet.go (about)

     1  // Copyright 2021 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tencentcloud
    16  
    17  import (
    18  	"strconv"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
    22  	vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
    23  )
    24  
    25  type SubnetGenerator struct {
    26  	TencentCloudService
    27  }
    28  
    29  func (g *SubnetGenerator) InitResources() error {
    30  	args := g.GetArgs()
    31  	region := args["region"].(string)
    32  	credential := args["credential"].(common.Credential)
    33  	profile := NewTencentCloudClientProfile()
    34  	client, err := vpc.NewClient(&credential, region, profile)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	request := vpc.NewDescribeSubnetsRequest()
    40  	offset := 0
    41  	pageSize := 50
    42  	allSubnets := make([]*vpc.Subnet, 0)
    43  
    44  	for {
    45  		offsetString := strconv.Itoa(offset)
    46  		limitString := strconv.Itoa(pageSize)
    47  		request.Offset = &offsetString
    48  		request.Limit = &limitString
    49  		response, err := client.DescribeSubnets(request)
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		allSubnets = append(allSubnets, response.Response.SubnetSet...)
    55  		if len(response.Response.SubnetSet) < pageSize {
    56  			break
    57  		}
    58  		offset += pageSize
    59  	}
    60  
    61  	for _, subnet := range allSubnets {
    62  		resource := terraformutils.NewResource(
    63  			*subnet.SubnetId,
    64  			*subnet.SubnetName+"_"+*subnet.SubnetId,
    65  			"tencentcloud_subnet",
    66  			"tencentcloud",
    67  			map[string]string{},
    68  			[]string{},
    69  			map[string]interface{}{},
    70  		)
    71  		g.Resources = append(g.Resources, resource)
    72  	}
    73  
    74  	return nil
    75  }