github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/digitalocean/droplet_snapshot.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 digitalocean
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/digitalocean/godo"
    22  )
    23  
    24  type DropletSnapshotGenerator struct {
    25  	DigitalOceanService
    26  }
    27  
    28  func (g DropletSnapshotGenerator) listDropletSnapshots(ctx context.Context, client *godo.Client) ([]godo.Snapshot, error) {
    29  	list := []godo.Snapshot{}
    30  
    31  	// create options. initially, these will be blank
    32  	opt := &godo.ListOptions{}
    33  	for {
    34  		snapshots, resp, err := client.Snapshots.ListDroplet(ctx, opt)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  
    39  		list = append(list, snapshots...)
    40  
    41  		// if we are at the last page, break out the for loop
    42  		if resp.Links == nil || resp.Links.IsLastPage() {
    43  			break
    44  		}
    45  
    46  		page, err := resp.Links.CurrentPage()
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  
    51  		// set the page we want for the next request
    52  		opt.Page = page + 1
    53  	}
    54  
    55  	return list, nil
    56  }
    57  
    58  func (g DropletSnapshotGenerator) createResources(snapshotList []godo.Snapshot) []terraformutils.Resource {
    59  	var resources []terraformutils.Resource
    60  	for _, snapshot := range snapshotList {
    61  		resources = append(resources, terraformutils.NewSimpleResource(
    62  			snapshot.ID,
    63  			snapshot.Name,
    64  			"digitalocean_droplet_snapshot",
    65  			"digitalocean",
    66  			[]string{}))
    67  	}
    68  	return resources
    69  }
    70  
    71  func (g *DropletSnapshotGenerator) InitResources() error {
    72  	client := g.generateClient()
    73  	output, err := g.listDropletSnapshots(context.TODO(), client)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	g.Resources = g.createResources(output)
    78  	return nil
    79  }