github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/yandex/compute_instance.go (about)

     1  // Copyright 2019 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 yandex
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
    22  	ycsdk "github.com/yandex-cloud/go-sdk"
    23  )
    24  
    25  type InstanceGenerator struct {
    26  	YandexService
    27  }
    28  
    29  func (g *InstanceGenerator) loadInstances(sdk *ycsdk.SDK, folderID string) ([]*compute.Instance, error) {
    30  	instances := []*compute.Instance{}
    31  	pageToken := ""
    32  	for {
    33  		resp, err := sdk.Compute().Instance().List(context.Background(), &compute.ListInstancesRequest{
    34  			FolderId:  folderID,
    35  			PageSize:  defaultPageSize,
    36  			PageToken: pageToken,
    37  		})
    38  
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  
    43  		instances = append(instances, resp.GetInstances()...)
    44  
    45  		if resp.GetNextPageToken() == "" {
    46  			break
    47  		}
    48  
    49  	}
    50  	return instances, nil
    51  
    52  }
    53  
    54  func (g *InstanceGenerator) InitResources() error {
    55  	sdk, err := ycsdk.Build(context.Background(), ycsdk.Config{
    56  		Credentials: ycsdk.OAuthToken(g.Args["token"].(string)),
    57  	})
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	result, err := g.loadInstances(sdk, g.Args["folder_id"].(string))
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	g.Resources = g.createResources(result)
    68  
    69  	return nil
    70  }
    71  
    72  func (g *InstanceGenerator) createResources(instances []*compute.Instance) []terraformutils.Resource {
    73  	var resources []terraformutils.Resource
    74  	for _, instance := range instances {
    75  		resources = append(resources, terraformutils.NewSimpleResource(
    76  			instance.GetId(),
    77  			instance.GetId(),
    78  			"yandex_compute_instance",
    79  			"yandex",
    80  			[]string{}))
    81  	}
    82  	return resources
    83  }