github.com/coreos/mantle@v0.13.0/platform/machine/openstack/flight.go (about)

     1  // Copyright 2018 Red Hat
     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  	ctplatform "github.com/coreos/container-linux-config-transpiler/config/platform"
    19  	"github.com/coreos/pkg/capnslog"
    20  
    21  	"github.com/coreos/mantle/platform"
    22  	"github.com/coreos/mantle/platform/api/openstack"
    23  )
    24  
    25  const (
    26  	Platform platform.Name = "openstack"
    27  )
    28  
    29  var (
    30  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/machine/openstack")
    31  )
    32  
    33  type flight struct {
    34  	*platform.BaseFlight
    35  	api      *openstack.API
    36  	keyAdded bool
    37  }
    38  
    39  // NewFlight creates an instance of a Flight suitable for spawning
    40  // instances on the OpenStack platform.
    41  func NewFlight(opts *openstack.Options) (platform.Flight, error) {
    42  	api, err := openstack.New(opts)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	bf, err := platform.NewBaseFlight(opts.Options, Platform, ctplatform.OpenStackMetadata)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	of := &flight{
    53  		BaseFlight: bf,
    54  		api:        api,
    55  	}
    56  
    57  	keys, err := of.Keys()
    58  	if err != nil {
    59  		of.Destroy()
    60  		return nil, err
    61  	}
    62  
    63  	if err := api.AddKey(of.Name(), keys[0].String()); err != nil {
    64  		of.Destroy()
    65  		return nil, err
    66  	}
    67  	of.keyAdded = true
    68  
    69  	return of, nil
    70  }
    71  
    72  // NewCluster creates an instance of a Cluster suitable for spawning
    73  // instances on the OpenStack platform.
    74  func (of *flight) NewCluster(rconf *platform.RuntimeConfig) (platform.Cluster, error) {
    75  	bc, err := platform.NewBaseCluster(of.BaseFlight, rconf)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	oc := &cluster{
    81  		BaseCluster: bc,
    82  		flight:      of,
    83  	}
    84  
    85  	of.AddCluster(oc)
    86  
    87  	return oc, nil
    88  }
    89  
    90  func (of *flight) Destroy() {
    91  	if of.keyAdded {
    92  		if err := of.api.DeleteKey(of.Name()); err != nil {
    93  			plog.Errorf("Error deleting key %v: %v", of.Name(), err)
    94  		}
    95  	}
    96  
    97  	of.BaseFlight.Destroy()
    98  }