github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/openstack/export_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  
    10  	"gopkg.in/goose.v2/neutron"
    11  	"gopkg.in/goose.v2/nova"
    12  	"gopkg.in/goose.v2/swift"
    13  
    14  	"github.com/juju/juju/core/instance"
    15  	"github.com/juju/juju/environs"
    16  	"github.com/juju/juju/environs/context"
    17  	"github.com/juju/juju/environs/instances"
    18  	envstorage "github.com/juju/juju/environs/storage"
    19  	"github.com/juju/juju/storage"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  var (
    24  	ShortAttempt   = &shortAttempt
    25  	StorageAttempt = &storageAttempt
    26  	CinderAttempt  = &cinderAttempt
    27  )
    28  
    29  func InstanceServerDetail(inst instances.Instance) *nova.ServerDetail {
    30  	return inst.(*openstackInstance).serverDetail
    31  }
    32  
    33  func InstanceFloatingIP(inst instances.Instance) *string {
    34  	return inst.(*openstackInstance).floatingIP
    35  }
    36  
    37  var (
    38  	NovaListAvailabilityZones   = &novaListAvailabilityZones
    39  	AvailabilityZoneAllocations = &availabilityZoneAllocations
    40  	NewOpenstackStorage         = &newOpenstackStorage
    41  )
    42  
    43  func NewCinderVolumeSource(s OpenstackStorage) storage.VolumeSource {
    44  	return NewCinderVolumeSourceForModel(s, testing.ModelTag.Id())
    45  }
    46  
    47  func NewCinderVolumeSourceForModel(s OpenstackStorage, modelUUID string) storage.VolumeSource {
    48  	const envName = "testmodel"
    49  	return &cinderVolumeSource{
    50  		storageAdapter: s,
    51  		envName:        envName,
    52  		modelUUID:      modelUUID,
    53  		namespace:      fakeNamespace{},
    54  	}
    55  }
    56  
    57  type fakeNamespace struct {
    58  	instance.Namespace
    59  }
    60  
    61  func (fakeNamespace) Value(s string) string {
    62  	return "juju-" + s
    63  }
    64  
    65  func SetUpGlobalGroup(e environs.Environ, ctx context.ProviderCallContext, name string, apiPort int) (neutron.SecurityGroupV2, error) {
    66  	switching := e.(*Environ).firewaller.(*switchingFirewaller)
    67  	if err := switching.initFirewaller(ctx); err != nil {
    68  		return neutron.SecurityGroupV2{}, err
    69  	}
    70  	return switching.fw.(*neutronFirewaller).setUpGlobalGroup(name, apiPort)
    71  }
    72  
    73  func EnsureGroup(e environs.Environ, ctx context.ProviderCallContext, name string, rules []neutron.RuleInfoV2) (neutron.SecurityGroupV2, error) {
    74  	switching := e.(*Environ).firewaller.(*switchingFirewaller)
    75  	if err := switching.initFirewaller(ctx); err != nil {
    76  		return neutron.SecurityGroupV2{}, err
    77  	}
    78  	return switching.fw.(*neutronFirewaller).ensureGroup(name, rules)
    79  }
    80  
    81  func MachineGroupRegexp(e environs.Environ, machineId string) string {
    82  	switching := e.(*Environ).firewaller.(*switchingFirewaller)
    83  	return switching.fw.(*neutronFirewaller).machineGroupRegexp(machineId)
    84  }
    85  
    86  func MachineGroupName(e environs.Environ, controllerUUID, machineId string) string {
    87  	switching := e.(*Environ).firewaller.(*switchingFirewaller)
    88  	return switching.fw.(*neutronFirewaller).machineGroupName(controllerUUID, machineId)
    89  }
    90  
    91  func MatchingGroup(e environs.Environ, ctx context.ProviderCallContext, nameRegExp string) (neutron.SecurityGroupV2, error) {
    92  	switching := e.(*Environ).firewaller.(*switchingFirewaller)
    93  	if err := switching.initFirewaller(ctx); err != nil {
    94  		return neutron.SecurityGroupV2{}, err
    95  	}
    96  	return switching.fw.(*neutronFirewaller).matchingGroup(ctx, nameRegExp)
    97  }
    98  
    99  // ImageMetadataStorage returns a Storage object pointing where the goose
   100  // infrastructure sets up its keystone entry for image metadata
   101  func ImageMetadataStorage(e environs.Environ) envstorage.Storage {
   102  	env := e.(*Environ)
   103  	return &openstackstorage{
   104  		containerName: "imagemetadata",
   105  		swift:         swift.New(env.clientUnlocked),
   106  	}
   107  }
   108  
   109  // CreateCustomStorage creates a swift container and returns the Storage object
   110  // so you can put data into it.
   111  func CreateCustomStorage(e environs.Environ, containerName string) envstorage.Storage {
   112  	env := e.(*Environ)
   113  	swiftClient := swift.New(env.clientUnlocked)
   114  	if err := swiftClient.CreateContainer(containerName, swift.PublicRead); err != nil {
   115  		panic(err)
   116  	}
   117  	return &openstackstorage{
   118  		containerName: containerName,
   119  		swift:         swiftClient,
   120  	}
   121  }
   122  
   123  // BlankContainerStorage creates a Storage object with blank container name.
   124  func BlankContainerStorage() envstorage.Storage {
   125  	return &openstackstorage{}
   126  }
   127  
   128  // GetNeutronClient returns the neutron client for the current environs.
   129  func GetNeutronClient(e environs.Environ) *neutron.Client {
   130  	return e.(*Environ).neutron()
   131  }
   132  
   133  // GetNovaClient returns the nova client for the current environs.
   134  func GetNovaClient(e environs.Environ) *nova.Client {
   135  	return e.(*Environ).nova()
   136  }
   137  
   138  // ResolveNetwork exposes environ helper function resolveNetwork for testing
   139  func ResolveNetwork(e environs.Environ, networkName string, external bool) (string, error) {
   140  	return e.(*Environ).networking.ResolveNetwork(networkName, external)
   141  }
   142  
   143  var PortsToRuleInfo = rulesToRuleInfo
   144  var SecGroupMatchesIngressRule = secGroupMatchesIngressRule
   145  
   146  var MakeServiceURL = &makeServiceURL
   147  
   148  var GetVolumeEndpointURL = getVolumeEndpointURL
   149  
   150  func GetModelGroupNames(e environs.Environ) ([]string, error) {
   151  	env := e.(*Environ)
   152  	rawFirewaller := env.firewaller.(*switchingFirewaller).fw
   153  	neutronFw, ok := rawFirewaller.(*neutronFirewaller)
   154  	if !ok {
   155  		return nil, fmt.Errorf("requires an env with a neutron firewaller")
   156  	}
   157  	groups, err := env.neutron().ListSecurityGroupsV2()
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	modelPattern, err := regexp.Compile(neutronFw.jujuGroupRegexp())
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  	var results []string
   166  	for _, group := range groups {
   167  		if modelPattern.MatchString(group.Name) {
   168  			results = append(results, group.Name)
   169  		}
   170  	}
   171  	return results, nil
   172  }
   173  
   174  func GetFirewaller(e environs.Environ) Firewaller {
   175  	env := e.(*Environ)
   176  	return env.firewaller
   177  }