github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/vpc.go (about) 1 // Copyright 2018 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 alicloud 16 17 import ( 18 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 19 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" 20 "github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" 21 ) 22 23 // VpcGenerator Struct for generating AliCloud Elastic Compute Service 24 type VpcGenerator struct { 25 AliCloudService 26 } 27 28 func resourceFromVpcResponse(vpc vpc.Vpc) terraformutils.Resource { 29 return terraformutils.NewResource( 30 vpc.VpcId, // id 31 vpc.VpcId+"__"+vpc.VpcName, // name 32 "alicloud_vpc", 33 "alicloud", 34 map[string]string{}, 35 []string{}, 36 map[string]interface{}{}, 37 ) 38 } 39 40 // InitResources Gets the list of all vpc Vpc ids and generates resources 41 func (g *VpcGenerator) InitResources() error { 42 client, err := g.LoadClientFromProfile() 43 if err != nil { 44 return err 45 } 46 remaining := 1 47 pageNumber := 1 48 pageSize := 10 49 50 allVpcs := make([]vpc.Vpc, 0) 51 52 for remaining > 0 { 53 raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) { 54 request := vpc.CreateDescribeVpcsRequest() 55 request.RegionId = client.RegionID 56 request.PageSize = requests.NewInteger(pageSize) 57 request.PageNumber = requests.NewInteger(pageNumber) 58 return vpcClient.DescribeVpcs(request) 59 }) 60 if err != nil { 61 return err 62 } 63 64 response := raw.(*vpc.DescribeVpcsResponse) 65 allVpcs = append(allVpcs, response.Vpcs.Vpc...) 66 remaining = response.TotalCount - pageNumber*pageSize 67 pageNumber++ 68 } 69 70 for _, Vpc := range allVpcs { 71 resource := resourceFromVpcResponse(Vpc) 72 g.Resources = append(g.Resources, resource) 73 } 74 75 return nil 76 }