github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/openstack/blockstorage.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 openstack
    16  
    17  import (
    18  	"log"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/gophercloud/gophercloud"
    22  	"github.com/gophercloud/gophercloud/openstack"
    23  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
    24  	"github.com/gophercloud/gophercloud/pagination"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  var resourceType = map[string]string{
    29  	"volumev3": "openstack_blockstorage_volume_v3",
    30  	"volumev2": "openstack_blockstorage_volume_v2",
    31  	"volume":   "openstack_blockstorage_volume_v1",
    32  }
    33  
    34  type BlockStorageGenerator struct {
    35  	OpenStackService
    36  }
    37  
    38  // createResources iterate on all openstack_blockstorage_volume
    39  func (g *BlockStorageGenerator) createResources(list *pagination.Pager, clientType string) []terraformutils.Resource {
    40  	resources := []terraformutils.Resource{}
    41  
    42  	err := list.EachPage(func(page pagination.Page) (bool, error) {
    43  		volumes, err := volumes.ExtractVolumes(page)
    44  		if err != nil {
    45  			return false, err
    46  		}
    47  
    48  		for _, v := range volumes {
    49  			// Use volume ID as a name if the volume doesn't have a name
    50  			name := v.Name
    51  			if v.Name == "" {
    52  				name = v.ID
    53  			}
    54  
    55  			resource := terraformutils.NewSimpleResource(
    56  				v.ID,
    57  				name,
    58  				resourceType[clientType],
    59  				"openstack",
    60  				[]string{},
    61  			)
    62  
    63  			resources = append(resources, resource)
    64  		}
    65  
    66  		return true, nil
    67  	})
    68  	if err != nil {
    69  		log.Println(err)
    70  	}
    71  
    72  	return resources
    73  }
    74  
    75  // Creates a BlockStorage ServiceClient
    76  func newBlockStorageClent(provider *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
    77  	// Choose v3 client firstly
    78  	if client, err := openstack.NewBlockStorageV3(provider, eo); err == nil {
    79  		log.Println("Using BlockStorage API v3")
    80  		return client, nil
    81  	}
    82  
    83  	// if it can't initialize v3 client, try to initialize v2 client
    84  	if client, err := openstack.NewBlockStorageV2(provider, eo); err == nil {
    85  		log.Println("Using BlockStorage API v2")
    86  		return client, nil
    87  	}
    88  
    89  	// if it can't initialize v2 client, try to initialize v1 client
    90  	if client, err := openstack.NewBlockStorageV1(provider, eo); err == nil {
    91  		log.Println("Using BlockStorage API v1")
    92  		return client, nil
    93  	}
    94  
    95  	return nil, errors.New("Failed to initialize BlockStorage client")
    96  }
    97  
    98  // Generate TerraformResources from OpenStack API,
    99  func (g *BlockStorageGenerator) InitResources() error {
   100  	opts, err := openstack.AuthOptionsFromEnv()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	provider, err := openstack.AuthenticatedClient(opts)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	eo := gophercloud.EndpointOpts{
   111  		Region: g.GetArgs()["region"].(string),
   112  	}
   113  
   114  	client, err := newBlockStorageClent(provider, eo)
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	list := volumes.List(client, nil)
   120  
   121  	g.Resources = g.createResources(&list, client.Type)
   122  
   123  	return nil
   124  }