github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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 "regexp" 8 9 "github.com/go-goose/goose/v5/neutron" 10 "github.com/go-goose/goose/v5/nova" 11 "github.com/go-goose/goose/v5/swift" 12 "github.com/juju/collections/set" 13 "github.com/juju/collections/transform" 14 "github.com/juju/errors" 15 16 "github.com/juju/juju/core/instance" 17 "github.com/juju/juju/environs" 18 "github.com/juju/juju/environs/context" 19 "github.com/juju/juju/environs/instances" 20 envstorage "github.com/juju/juju/environs/storage" 21 "github.com/juju/juju/provider/common" 22 "github.com/juju/juju/storage" 23 "github.com/juju/juju/testing" 24 ) 25 26 var ( 27 ShortAttempt = &shortAttempt 28 StorageAttempt = &storageAttempt 29 CinderAttempt = &cinderAttempt 30 ) 31 32 func InstanceServerDetail(inst instances.Instance) *nova.ServerDetail { 33 return inst.(*openstackInstance).serverDetail 34 } 35 36 func InstanceFloatingIP(inst instances.Instance) *string { 37 return inst.(*openstackInstance).floatingIP 38 } 39 40 var ( 41 NovaListAvailabilityZones = &novaListAvailabilityZones 42 NewOpenstackStorage = &newOpenstackStorage 43 ) 44 45 func NewCinderVolumeSource(s OpenstackStorage, env common.ZonedEnviron) storage.VolumeSource { 46 return NewCinderVolumeSourceForModel(s, testing.ModelTag.Id(), env) 47 } 48 49 func NewCinderVolumeSourceForModel(s OpenstackStorage, modelUUID string, env common.ZonedEnviron) storage.VolumeSource { 50 const envName = "testmodel" 51 return &cinderVolumeSource{ 52 storageAdapter: s, 53 envName: envName, 54 modelUUID: modelUUID, 55 namespace: fakeNamespace{}, 56 zonedEnv: env, 57 } 58 } 59 60 type fakeNamespace struct { 61 instance.Namespace 62 } 63 64 func (fakeNamespace) Value(s string) string { 65 return "juju-" + s 66 } 67 68 func EnsureGroup(e environs.Environ, ctx context.ProviderCallContext, name string, isModelGroup bool) (neutron.SecurityGroupV2, error) { 69 switching := &neutronFirewaller{firewallerBase: firewallerBase{environ: e.(*Environ)}} 70 return switching.ensureGroup(name, isModelGroup) 71 } 72 73 func MachineGroupRegexp(e environs.Environ, machineId string) string { 74 switching := &neutronFirewaller{firewallerBase: firewallerBase{environ: e.(*Environ)}} 75 return switching.machineGroupRegexp(machineId) 76 } 77 78 func MachineGroupName(e environs.Environ, controllerUUID, machineId string) string { 79 switching := &neutronFirewaller{firewallerBase: firewallerBase{environ: e.(*Environ)}} 80 return switching.machineGroupName(controllerUUID, machineId) 81 } 82 83 func MatchingGroup(e environs.Environ, ctx context.ProviderCallContext, nameRegExp string) (neutron.SecurityGroupV2, error) { 84 switching := &neutronFirewaller{firewallerBase: firewallerBase{environ: e.(*Environ)}} 85 return switching.matchingGroup(ctx, nameRegExp) 86 } 87 88 // ImageMetadataStorage returns a Storage object pointing where the goose 89 // infrastructure sets up its keystone entry for image metadata 90 func ImageMetadataStorage(e environs.Environ) envstorage.Storage { 91 env := e.(*Environ) 92 return &openstackstorage{ 93 containerName: "imagemetadata", 94 swift: swift.New(env.clientUnlocked), 95 } 96 } 97 98 // CreateCustomStorage creates a swift container and returns the Storage object 99 // so you can put data into it. 100 func CreateCustomStorage(e environs.Environ, containerName string) envstorage.Storage { 101 env := e.(*Environ) 102 swiftClient := swift.New(env.clientUnlocked) 103 if err := swiftClient.CreateContainer(containerName, swift.PublicRead); err != nil { 104 panic(err) 105 } 106 return &openstackstorage{ 107 containerName: containerName, 108 swift: swiftClient, 109 } 110 } 111 112 // BlankContainerStorage creates a Storage object with blank container name. 113 func BlankContainerStorage() envstorage.Storage { 114 return &openstackstorage{} 115 } 116 117 // GetNeutronClient returns the neutron client for the current environs. 118 func GetNeutronClient(e environs.Environ) *neutron.Client { 119 return e.(*Environ).neutron() 120 } 121 122 // GetNovaClient returns the nova client for the current environs. 123 func GetNovaClient(e environs.Environ) *nova.Client { 124 return e.(*Environ).nova() 125 } 126 127 // ResolveNetworks exposes environ helper function resolveNetwork for testing 128 func ResolveNetworkIDs(e environs.Environ, networkName string, external bool) ([]string, error) { 129 networks, err := e.(*Environ).networking.ResolveNetworks(networkName, external) 130 toID := func(n neutron.NetworkV2) string { return n.Id } 131 return transform.Slice(networks, toID), errors.Trace(err) 132 } 133 134 // FindNetworks exposes environ helper function FindNetworks for testing 135 func FindNetworks(e environs.Environ, internal bool) (set.Strings, error) { 136 return e.(*Environ).networking.FindNetworks(internal) 137 } 138 139 var PortsToRuleInfo = rulesToRuleInfo 140 var SecGroupMatchesIngressRule = secGroupMatchesIngressRule 141 142 var MakeServiceURL = &makeServiceURL 143 144 var GetVolumeEndpointURL = getVolumeEndpointURL 145 146 func GetModelGroupNames(e environs.Environ) ([]string, error) { 147 env := e.(*Environ) 148 neutronFw := env.firewaller.(*neutronFirewaller) 149 groups, err := env.neutron().ListSecurityGroupsV2() 150 if err != nil { 151 return nil, err 152 } 153 modelPattern, err := regexp.Compile(neutronFw.jujuGroupPrefixRegexp()) 154 if err != nil { 155 return nil, err 156 } 157 var results []string 158 for _, group := range groups { 159 if modelPattern.MatchString(group.Name) { 160 results = append(results, group.Name) 161 } 162 } 163 return results, nil 164 } 165 166 func GetFirewaller(e environs.Environ) Firewaller { 167 env := e.(*Environ) 168 return env.firewaller 169 }