github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/as.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 as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419" 20 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" 21 ) 22 23 type AsGenerator struct { 24 TencentCloudService 25 } 26 27 func (g *AsGenerator) InitResources() error { 28 args := g.GetArgs() 29 region := args["region"].(string) 30 credential := args["credential"].(common.Credential) 31 profile := NewTencentCloudClientProfile() 32 client, err := as.NewClient(&credential, region, profile) 33 if err != nil { 34 return err 35 } 36 37 if err := g.loadScalingGroups(client); err != nil { 38 return err 39 } 40 if err := g.loadScalingConfigs(client); err != nil { 41 return err 42 } 43 44 return nil 45 } 46 47 func (g *AsGenerator) loadScalingGroups(client *as.Client) error { 48 request := as.NewDescribeAutoScalingGroupsRequest() 49 50 var offset uint64 = 0 51 var pageSize uint64 = 50 52 allInstances := make([]*as.AutoScalingGroup, 0) 53 54 for { 55 request.Offset = &offset 56 request.Limit = &pageSize 57 response, err := client.DescribeAutoScalingGroups(request) 58 if err != nil { 59 return err 60 } 61 62 allInstances = append(allInstances, response.Response.AutoScalingGroupSet...) 63 if len(response.Response.AutoScalingGroupSet) < int(pageSize) { 64 break 65 } 66 offset += pageSize 67 } 68 69 for _, instance := range allInstances { 70 resource := terraformutils.NewResource( 71 *instance.AutoScalingGroupId, 72 *instance.AutoScalingGroupName+"_"+*instance.AutoScalingGroupId, 73 "tencentcloud_as_scaling_group", 74 "tencentcloud", 75 map[string]string{}, 76 []string{}, 77 map[string]interface{}{}, 78 ) 79 g.Resources = append(g.Resources, resource) 80 } 81 82 return nil 83 } 84 85 func (g *AsGenerator) loadScalingConfigs(client *as.Client) error { 86 request := as.NewDescribeLaunchConfigurationsRequest() 87 88 var offset uint64 = 0 89 var pageSize uint64 = 50 90 allInstances := make([]*as.LaunchConfiguration, 0) 91 92 for { 93 request.Offset = &offset 94 request.Limit = &pageSize 95 response, err := client.DescribeLaunchConfigurations(request) 96 if err != nil { 97 return err 98 } 99 100 allInstances = append(allInstances, response.Response.LaunchConfigurationSet...) 101 if len(response.Response.LaunchConfigurationSet) < int(pageSize) { 102 break 103 } 104 offset += pageSize 105 } 106 107 for _, instance := range allInstances { 108 resource := terraformutils.NewResource( 109 *instance.LaunchConfigurationId, 110 *instance.LaunchConfigurationName+"_"+*instance.LaunchConfigurationId, 111 "tencentcloud_as_scaling_config", 112 "tencentcloud", 113 map[string]string{}, 114 []string{}, 115 map[string]interface{}{}, 116 ) 117 g.Resources = append(g.Resources, resource) 118 } 119 120 return nil 121 } 122 123 func (g *AsGenerator) PostConvertHook() error { 124 for i, resource := range g.Resources { 125 if resource.InstanceInfo.Type != "tencentcloud_as_scaling_group" { 126 continue 127 } 128 if configID, exist := resource.InstanceState.Attributes["configuration_id"]; exist { 129 for _, r := range g.Resources { 130 if r.InstanceInfo.Type != "tencentcloud_as_scaling_config" { 131 continue 132 } 133 if configID == r.InstanceState.Attributes["id"] { 134 g.Resources[i].Item["configuration_id"] = "${tencentcloud_as_scaling_config." + r.ResourceName + ".id}" 135 } 136 } 137 } 138 } 139 140 return nil 141 }