github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/yandex/compute_disk.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 DiskGenerator struct { 26 YandexService 27 } 28 29 func (g *DiskGenerator) loadDisks(sdk *ycsdk.SDK, folderID string) ([]*compute.Disk, error) { 30 disks := []*compute.Disk{} 31 pageToken := "" 32 for { 33 resp, err := sdk.Compute().Disk().List(context.Background(), &compute.ListDisksRequest{ 34 FolderId: folderID, 35 PageSize: defaultPageSize, 36 PageToken: pageToken, 37 }) 38 39 if err != nil { 40 return nil, err 41 } 42 43 disks = append(disks, resp.GetDisks()...) 44 45 if resp.GetNextPageToken() == "" { 46 break 47 } 48 49 } 50 return disks, nil 51 52 } 53 54 func (g *DiskGenerator) 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.loadDisks(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 *DiskGenerator) createResources(disks []*compute.Disk) []terraformutils.Resource { 73 var resources []terraformutils.Resource 74 for _, disk := range disks { 75 resources = append(resources, terraformutils.NewSimpleResource( 76 disk.GetId(), 77 disk.GetId(), 78 "yandex_compute_disk", 79 "yandex", 80 []string{})) 81 } 82 return resources 83 }