github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/vswitch.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  // VSwitchGenerator Struct for generating AliCloud Elastic Compute Service
    24  type VSwitchGenerator struct {
    25  	AliCloudService
    26  }
    27  
    28  func resourceFromVSwitchResponse(vswitch vpc.VSwitch) terraformutils.Resource {
    29  	return terraformutils.NewResource(
    30  		vswitch.VSwitchId, // nolint
    31  		vswitch.VSwitchId+"__"+vswitch.VSwitchName, // nolint
    32  		"alicloud_vswitch",
    33  		"alicloud",
    34  		map[string]string{},
    35  		[]string{},
    36  		map[string]interface{}{},
    37  	)
    38  }
    39  
    40  // InitResources Gets the list of all vpc VSwitch ids and generates resources
    41  func (g *VSwitchGenerator) 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  	allVSwitchs := make([]vpc.VSwitch, 0)
    51  
    52  	for remaining > 0 {
    53  		raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
    54  			request := vpc.CreateDescribeVSwitchesRequest()
    55  			request.RegionId = client.RegionID
    56  			request.PageSize = requests.NewInteger(pageSize)
    57  			request.PageNumber = requests.NewInteger(pageNumber)
    58  			return vpcClient.DescribeVSwitches(request)
    59  		})
    60  		if err != nil {
    61  			return err
    62  		}
    63  
    64  		response := raw.(*vpc.DescribeVSwitchesResponse)
    65  		allVSwitchs = append(allVSwitchs, response.VSwitches.VSwitch...)
    66  		remaining = response.TotalCount - pageNumber*pageSize
    67  		pageNumber++
    68  	}
    69  
    70  	for _, VSwitch := range allVSwitchs {
    71  		resource := resourceFromVSwitchResponse(VSwitch)
    72  		g.Resources = append(g.Resources, resource)
    73  	}
    74  
    75  	return nil
    76  }