github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/mongodb.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  	mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20180408"
    21  )
    22  
    23  type MongodbGenerator struct {
    24  	TencentCloudService
    25  }
    26  
    27  func (g *MongodbGenerator) InitResources() error {
    28  	args := g.GetArgs()
    29  	region := args["region"].(string)
    30  	credential := args["credential"].(common.Credential)
    31  	profile := NewTencentCloudClientProfile()
    32  	client, err := mongodb.NewClient(&credential, region, profile)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	request := mongodb.NewDescribeDBInstancesRequest()
    38  
    39  	var offset uint64 = 0
    40  	var pageSize uint64 = 50
    41  	allInstances := make([]*mongodb.MongoDBInstanceDetail, 0)
    42  
    43  	for {
    44  		request.Offset = &offset
    45  		request.Limit = &pageSize
    46  		response, err := client.DescribeDBInstances(request)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		allInstances = append(allInstances, response.Response.InstanceDetails...)
    52  		if len(response.Response.InstanceDetails) < 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_mongodb_instance",
    63  			"tencentcloud",
    64  			map[string]string{},
    65  			[]string{},
    66  			map[string]interface{}{},
    67  		)
    68  		g.Resources = append(g.Resources, resource)
    69  	}
    70  
    71  	return nil
    72  }