-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
308 changed files
with
46,777 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
golang 1.21.3 | ||
golang 1.22.6 |
39 changes: 39 additions & 0 deletions
39
src/openstack_cpi_golang/cpi/compute/availbility_zone_provider.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package compute | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/cloudfoundry/bosh-openstack-cpi-release/src/openstack_cpi_golang/cpi/properties" | ||
) | ||
|
||
//counterfeiter:generate . AvailabilityZoneProvider | ||
type AvailabilityZoneProvider interface { | ||
GetAvailabilityZones(cloudProperties properties.CreateVM) []string | ||
} | ||
|
||
type availabilityZoneProvider struct { | ||
} | ||
|
||
func NewAvailabilityZoneProvider() availabilityZoneProvider { | ||
return availabilityZoneProvider{} | ||
} | ||
|
||
func (a availabilityZoneProvider) GetAvailabilityZones(cloudProperties properties.CreateVM) []string { | ||
|
||
if len(cloudProperties.AvailabilityZones) > 0 { | ||
return a.shuffleAZs(cloudProperties) | ||
} | ||
|
||
return []string{cloudProperties.AvailabilityZone} | ||
} | ||
|
||
func (a availabilityZoneProvider) shuffleAZs(cloudProperties properties.CreateVM) []string { | ||
shuffledZones := cloudProperties.AvailabilityZones | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
r.Shuffle(len(shuffledZones), func(i, j int) { | ||
shuffledZones[i], shuffledZones[j] = shuffledZones[j], shuffledZones[i] | ||
}) | ||
return shuffledZones | ||
} |
58 changes: 58 additions & 0 deletions
58
src/openstack_cpi_golang/cpi/compute/availbility_zone_provider_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package compute_test | ||
|
||
import ( | ||
"github.com/cloudfoundry/bosh-openstack-cpi-release/src/openstack_cpi_golang/cpi/compute" | ||
"github.com/cloudfoundry/bosh-openstack-cpi-release/src/openstack_cpi_golang/cpi/properties" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AvailabilityZoneProvider", func() { | ||
Context("GetAvailabilityZones", func() { | ||
|
||
var cloudProps properties.CreateVM | ||
|
||
Context("with cloud property availability_zones", func() { | ||
|
||
BeforeEach(func() { | ||
cloudProps = properties.CreateVM{} | ||
cloudProps.AvailabilityZone = "" | ||
}) | ||
|
||
It("returns a single availability zone", func() { | ||
cloudProps.AvailabilityZones = []string{"z1"} | ||
zone := compute.NewAvailabilityZoneProvider(). | ||
GetAvailabilityZones(cloudProps) | ||
|
||
Expect(len(zone)).To(Equal(1)) | ||
Expect(zone).To(ContainElement("z1")) | ||
}) | ||
|
||
It("returns a single availability zone", func() { | ||
cloudProps.AvailabilityZones = []string{"z1", "z2", "z3"} | ||
zone := compute.NewAvailabilityZoneProvider(). | ||
GetAvailabilityZones(cloudProps) | ||
|
||
Expect(len(zone)).To(Equal(3)) | ||
Expect(zone).To(ContainElements("z1", "z2", "z3")) | ||
}) | ||
}) | ||
|
||
Context("with cloud property availability_zone", func() { | ||
|
||
BeforeEach(func() { | ||
cloudProps = properties.CreateVM{} | ||
cloudProps.AvailabilityZones = []string{} | ||
}) | ||
|
||
It("returns a single availability zone", func() { | ||
cloudProps.AvailabilityZone = "z1" | ||
zone := compute.NewAvailabilityZoneProvider(). | ||
GetAvailabilityZones(cloudProps) | ||
|
||
Expect(len(zone)).To(Equal(1)) | ||
Expect(zone).To(ContainElement("z1")) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package compute | ||
|
||
import ( | ||
"github.com/cloudfoundry/bosh-openstack-cpi-release/src/openstack_cpi_golang/cpi/utils" | ||
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones" | ||
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" | ||
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" | ||
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors" | ||
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers" | ||
"github.com/gophercloud/gophercloud/pagination" | ||
) | ||
|
||
type ServerWithAZ struct { | ||
servers.Server | ||
availabilityzones.ServerAvailabilityZoneExt | ||
} | ||
|
||
//counterfeiter:generate . ComputeFacade | ||
type ComputeFacade interface { | ||
CreateServer(client utils.ServiceClient, opts servers.CreateOptsBuilder) (*servers.Server, error) | ||
|
||
DeleteServer(client utils.RetryableServiceClient, serverID string) error | ||
|
||
RebootServer(client utils.ServiceClient, serverID string, opts servers.RebootOptsBuilder) error | ||
|
||
GetServer(client utils.RetryableServiceClient, serverID string) (*servers.Server, error) | ||
|
||
GetServerWithAZ(client utils.RetryableServiceClient, serverID string) (*ServerWithAZ, error) | ||
|
||
ListFlavors(client utils.RetryableServiceClient, opts flavors.ListOpts) (pagination.Page, error) | ||
|
||
ExtractFlavors(page pagination.Page) ([]flavors.Flavor, error) | ||
|
||
GetOSKeyPair(client utils.RetryableServiceClient, keyPairName string, ops keypairs.GetOpts) (*keypairs.KeyPair, error) | ||
|
||
GetServerMetadata(client utils.RetryableServiceClient, serverID string) (map[string]string, error) | ||
|
||
UpdateServer(client utils.ServiceClient, serverID string, opt servers.UpdateOptsBuilder) (*servers.Server, error) | ||
|
||
UpdateServerMetadata(client utils.ServiceClient, serverID string, opts servers.UpdateMetadataOptsBuilder) (map[string]string, error) | ||
|
||
DeleteServerMetaData(client *gophercloud.ServiceClient, serverID string, key string) error | ||
|
||
AttachVolume(client *gophercloud.ServiceClient, serverID string, opts volumeattach.CreateOptsBuilder) (*volumeattach.VolumeAttachment, error) | ||
|
||
DetachVolume(client *gophercloud.ServiceClient, serverID string, volumeID string) error | ||
|
||
ListVolumeAttachments(client *gophercloud.ServiceClient, serverID string) ([]volumeattach.VolumeAttachment, error) | ||
} | ||
|
||
type computeFacade struct { | ||
} | ||
|
||
func NewComputeFacade() computeFacade { | ||
return computeFacade{} | ||
} | ||
|
||
func (c computeFacade) CreateServer(client utils.ServiceClient, opts servers.CreateOptsBuilder) (*servers.Server, error) { | ||
return servers.Create(client, opts).Extract() | ||
} | ||
|
||
func (c computeFacade) DeleteServer(client utils.RetryableServiceClient, serverID string) error { | ||
return servers.Delete(client, serverID).ExtractErr() | ||
} | ||
|
||
func (c computeFacade) RebootServer(client utils.ServiceClient, serverID string, opts servers.RebootOptsBuilder) error { | ||
return servers.Reboot(client, serverID, opts).ExtractErr() | ||
} | ||
|
||
func (c computeFacade) GetServer(client utils.RetryableServiceClient, serverID string) (*servers.Server, error) { | ||
return servers.Get(client, serverID).Extract() | ||
} | ||
|
||
func (c computeFacade) GetServerWithAZ(client utils.RetryableServiceClient, serverID string) (*ServerWithAZ, error) { | ||
var serverWithAz ServerWithAZ | ||
err := servers.Get(client, serverID).ExtractInto(&serverWithAz) | ||
return &serverWithAz, err | ||
} | ||
|
||
func (c computeFacade) ListFlavors(client utils.RetryableServiceClient, opts flavors.ListOpts) (pagination.Page, error) { | ||
return flavors.ListDetail(client, opts).AllPages() | ||
} | ||
|
||
func (c computeFacade) ExtractFlavors(page pagination.Page) ([]flavors.Flavor, error) { | ||
return flavors.ExtractFlavors(page) | ||
} | ||
|
||
func (c computeFacade) GetOSKeyPair(client utils.RetryableServiceClient, keyPairName string, opts keypairs.GetOpts) (*keypairs.KeyPair, error) { | ||
return keypairs.Get(client, keyPairName, opts).Extract() | ||
} | ||
|
||
func (c computeFacade) GetServerMetadata(client utils.RetryableServiceClient, serverID string) (map[string]string, error) { | ||
return servers.Metadata(client, serverID).Extract() | ||
} | ||
|
||
func (c computeFacade) UpdateServer(client utils.ServiceClient, serverID string, opts servers.UpdateOptsBuilder) (*servers.Server, error) { | ||
return servers.Update(client, serverID, opts).Extract() | ||
} | ||
|
||
func (c computeFacade) UpdateServerMetadata(client utils.ServiceClient, serverID string, opts servers.UpdateMetadataOptsBuilder) (map[string]string, error) { | ||
return servers.UpdateMetadata(client, serverID, opts).Extract() | ||
} | ||
|
||
func (c computeFacade) DeleteServerMetaData(client *gophercloud.ServiceClient, serverID string, key string) error { | ||
return servers.DeleteMetadatum(client, serverID, key).ExtractErr() | ||
} | ||
|
||
func (c computeFacade) AttachVolume(client *gophercloud.ServiceClient, serverID string, opts volumeattach.CreateOptsBuilder) (*volumeattach.VolumeAttachment, error) { | ||
return volumeattach.Create(client, serverID, opts).Extract() | ||
} | ||
|
||
func (c computeFacade) DetachVolume(client *gophercloud.ServiceClient, serverID string, volumeID string) error { | ||
return volumeattach.Delete(client, serverID, volumeID).ExtractErr() | ||
} | ||
|
||
func (c computeFacade) ListVolumeAttachments(client *gophercloud.ServiceClient, serverID string) ([]volumeattach.VolumeAttachment, error) { | ||
page, err := volumeattach.List(client, serverID).AllPages() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return volumeattach.ExtractVolumeAttachments(page) | ||
} |
Oops, something went wrong.