github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/cvm.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  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
    20  	cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
    21  )
    22  
    23  type CvmGenerator struct {
    24  	TencentCloudService
    25  }
    26  
    27  func (g *CvmGenerator) InitResources() error {
    28  	args := g.GetArgs()
    29  	region := args["region"].(string)
    30  	credential := args["credential"].(common.Credential)
    31  	profile := NewTencentCloudClientProfile()
    32  	client, err := cvm.NewClient(&credential, region, profile)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	request := cvm.NewDescribeInstancesRequest()
    38  
    39  	var offset int64 = 0
    40  	var pageSize int64 = 50
    41  	allInstances := make([]*cvm.Instance, 0)
    42  
    43  	for {
    44  		request.Offset = &offset
    45  		request.Limit = &pageSize
    46  		response, err := client.DescribeInstances(request)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		allInstances = append(allInstances, response.Response.InstanceSet...)
    52  		if len(response.Response.InstanceSet) < int(pageSize) {
    53  			break
    54  		}
    55  		offset += pageSize
    56  	}
    57  
    58  	for _, instance := range allInstances {
    59  		resource := terraformutils.NewResource(
    60  			*instance.InstanceId,
    61  			*instance.InstanceName+"_"+*instance.InstanceId,
    62  			"tencentcloud_instance",
    63  			"tencentcloud",
    64  			map[string]string{
    65  				"disable_monitor_service":  "false",
    66  				"disable_security_service": "false",
    67  				"force_delete":             "false",
    68  			},
    69  			[]string{},
    70  			map[string]interface{}{},
    71  		)
    72  		g.Resources = append(g.Resources, resource)
    73  	}
    74  
    75  	return nil
    76  }