github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/security_group.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  	"fmt"
    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 SecurityGroupGenerator struct {
    26  	TencentCloudService
    27  }
    28  
    29  func (g *SecurityGroupGenerator) 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.NewDescribeSecurityGroupsRequest()
    40  
    41  	var offset int64 = 0
    42  	var pageSize int64 = 50
    43  	allInstances := make([]*vpc.SecurityGroup, 0)
    44  
    45  	for {
    46  		offsetString := fmt.Sprintf("%d", offset)
    47  		limitString := fmt.Sprintf("%d", pageSize)
    48  		request.Offset = &offsetString
    49  		request.Limit = &limitString
    50  		response, err := client.DescribeSecurityGroups(request)
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		allInstances = append(allInstances, response.Response.SecurityGroupSet...)
    56  		if len(response.Response.SecurityGroupSet) < int(pageSize) {
    57  			break
    58  		}
    59  		offset += pageSize
    60  	}
    61  
    62  	for _, instance := range allInstances {
    63  		resource := terraformutils.NewResource(
    64  			*instance.SecurityGroupId,
    65  			*instance.SecurityGroupName+"_"+*instance.SecurityGroupId,
    66  			"tencentcloud_security_group",
    67  			"tencentcloud",
    68  			map[string]string{},
    69  			[]string{},
    70  			map[string]interface{}{},
    71  		)
    72  		g.Resources = append(g.Resources, resource)
    73  	}
    74  
    75  	return nil
    76  }