Skip to content

Commit 3667df3

Browse files
rinoreyberg
andauthored
refactor(provider): Unify new provider instance method (#1425)
* refactor(provider): Unify new provider instance method * refactor(provider): remove some hard-coded values and direct initialization --------- Co-authored-by: Ian Eyberg <[email protected]>
1 parent b5d4fa6 commit 3667df3

File tree

19 files changed

+183
-91
lines changed

19 files changed

+183
-91
lines changed

cmd/cmd_image.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func imageCreateCommandHandler(cmd *cobra.Command, args []string) {
118118
}
119119

120120
imageName := c.CloudConfig.ImageName
121-
if c.CloudConfig.Platform == "onprem" {
121+
if c.CloudConfig.Platform == onprem.ProviderName {
122122
imageName = c.RunConfig.Imagename
123123
}
124124

@@ -365,15 +365,15 @@ func imageSyncCommand() *cobra.Command {
365365
Run: imageSyncCommandHandler,
366366
Args: cobra.MinimumNArgs(1),
367367
}
368-
cmdImageSync.PersistentFlags().StringVarP(&sourceCloud, "source-cloud", "s", "onprem", "cloud platform [gcp, aws, do, vultr, onprem, hyper-v, upcloud]")
368+
cmdImageSync.PersistentFlags().StringVarP(&sourceCloud, "source-cloud", "s", onprem.ProviderName, "cloud platform [gcp, aws, do, vultr, onprem, hyper-v, upcloud]")
369369
return cmdImageSync
370370
}
371371

372372
func imageSyncCommandHandler(cmd *cobra.Command, args []string) {
373373
image := args[0]
374374
// TODO only accepts onprem for now, implement for other source providers later
375375
source, _ := cmd.Flags().GetString("source-cloud")
376-
if source != "onprem" {
376+
if source != onprem.ProviderName {
377377
exitWithError(source + " sync not yet implemented")
378378
}
379379

@@ -655,7 +655,7 @@ func getLocalImageReader(flags *pflag.FlagSet, args []string) *fs.Reader {
655655
if err != nil {
656656
exitWithError(err.Error())
657657
}
658-
if c.CloudConfig.Platform != "onprem" {
658+
if c.CloudConfig.Platform != onprem.ProviderName {
659659
exitWithError("Image subcommand not implemented yet for cloud images")
660660
}
661661
imageName := args[0]

provider/aws/aws.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"github.com/nanovms/ops/types"
1616
)
1717

18-
// AWS contains all operations for AWS
18+
// ProviderName of the cloud platform provider
19+
const ProviderName = "aws"
20+
21+
// AWS Provider to interact with AWS cloud infrastructure
1922
type AWS struct {
2023
Storage *S3
2124
dnsService *route53.Route53
@@ -24,6 +27,11 @@ type AWS struct {
2427
ec2 *ec2.EC2
2528
}
2629

30+
// NewProvider AWS
31+
func NewProvider() *AWS {
32+
return &AWS{}
33+
}
34+
2735
// strips any zone qualifier from 'zone' string
2836
// some AWS API calls only want region even if full region-zone is used
2937
// elsewhere in the same call

provider/aws/aws_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (p *AWS) GetVPC(ctx *lepton.Context, svc *ec2.EC2) (*ec2.Vpc, error) {
227227
return vpc, nil
228228
}
229229

230-
func (p AWS) buildFirewallRule(protocol string, port string, ipv4, ipv6 bool) *ec2.IpPermission {
230+
func (p *AWS) buildFirewallRule(protocol string, port string, ipv4, ipv6 bool) *ec2.IpPermission {
231231
fromPort := port
232232
toPort := port
233233

provider/azure/azure.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
2121
)
2222

23+
// ProviderName of the cloud platform provider
24+
const ProviderName = "azure"
25+
2326
// most of this is ripped from the samples repo:
2427
// https://github.com/Azure-Samples/azure-sdk-for-go-samples/blob/master/compute/vm.go
2528
// the azure sdk is fairly round-a-bout and could use some heavy
@@ -35,7 +38,7 @@ var (
3538
cloudName = "AzurePublicCloud"
3639
)
3740

38-
// Azure contains all operations for Azure
41+
// Azure Provider to interact with Azure cloud infrastructure
3942
type Azure struct {
4043
Storage *Storage
4144
subID string
@@ -50,6 +53,11 @@ type Azure struct {
5053
authorizer *autorest.Authorizer
5154
}
5255

56+
// NewProvider Azure
57+
func NewProvider() *Azure {
58+
return &Azure{}
59+
}
60+
5361
func getAzureDefaultTags() map[string]*string {
5462
return map[string]*string{
5563
"CreatedAt": to.StringPtr(fmt.Sprintf(time.Now().Format(time.RFC3339))),

provider/digitalocean/digital_ocean.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import (
88
"github.com/nanovms/ops/types"
99
)
1010

11-
// DigitalOcean provides access to the DigitalOcean API.
11+
// ProviderName of the cloud platform provider
12+
const ProviderName = "do"
13+
14+
// DigitalOcean Provider to interact with DigitalOcean cloud infrastructure
1215
type DigitalOcean struct {
1316
Storage *Spaces
1417
Client *godo.Client
1518
}
1619

20+
// NewProvider DigitalOcean
21+
func NewProvider() *DigitalOcean {
22+
return &DigitalOcean{}
23+
}
24+
1725
// Initialize DigialOcean related things
1826
func (do *DigitalOcean) Initialize(c *types.ProviderConfig) error {
1927
doToken := os.Getenv("DO_TOKEN")

provider/gcp/gcp.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
dns "google.golang.org/api/dns/v1"
1717
)
1818

19+
// ProviderName of the cloud platform provider
20+
const ProviderName = "gcp"
21+
1922
var (
2023
errGCloudProjectIDMissing = func() error { return errors.New("projectid is missing. Please set env variable GCLOUD_PROJECT_ID") }
2124
errGCloudZoneMissing = func() error { return errors.New("zone is missing. Please set env variable GCLOUD_ZONE") }
@@ -83,7 +86,7 @@ func (gop *GCloudOperation) isDone(ctx context.Context) (bool, error) {
8386
return true, nil
8487
}
8588

86-
// GCloud contains all operations for GCP
89+
// GCloud Provider to interact with GCP cloud infrastructure
8790
type GCloud struct {
8891
Storage *Storage
8992
Service *compute.Service
@@ -92,8 +95,8 @@ type GCloud struct {
9295
dnsService *dns.Service
9396
}
9497

95-
// NewGCloud returns an instance of GCloud
96-
func NewGCloud() *GCloud {
98+
// NewProvider GCP
99+
func NewProvider() *GCloud {
97100
return &GCloud{}
98101
}
99102

provider/hyperv/hyperv.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"github.com/nanovms/ops/wsl"
99
)
1010

11-
// Provider provides access to the Hyper-V API.
12-
type Provider struct {
13-
}
11+
// ProviderName of the cloud platform provider
12+
const ProviderName = "hyper-v"
13+
14+
// Provider to interact with Hyper-V cloud infrastructure
15+
type Provider struct{}
1416

15-
// NewProvider returns an instance of Hyper-V provider
17+
// NewProvider Hyper-V
1618
func NewProvider() *Provider {
1719
return &Provider{}
1820
}

provider/oci/oci.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
"github.com/spf13/afero"
1313
)
1414

15+
// ProviderName of the cloud platform provider
16+
const ProviderName = "oci"
17+
1518
var (
1619
ociOpsTags = map[string]string{"CreatedBy": "OPS"}
1720
)
@@ -61,7 +64,7 @@ type BlockstorageService interface {
6164
DeleteVolume(ctx context.Context, request core.DeleteVolumeRequest) (response core.DeleteVolumeResponse, err error)
6265
}
6366

64-
// Provider has methods to interact with oracle cloud infrastructure
67+
// Provider to interact with Oracle Cloud Infrastructure
6568
type Provider struct {
6669
computeClient ComputeService
6770
storageClient StorageService
@@ -73,7 +76,7 @@ type Provider struct {
7376
availabilityDomain string
7477
}
7578

76-
// NewProvider returns an instance of OCI Provider
79+
// NewProvider OCI
7780
func NewProvider() *Provider {
7881
return &Provider{
7982
computeClient: nil,

provider/onprem/onprem.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import (
55
"github.com/nanovms/ops/types"
66
)
77

8-
// ProviderName is the name of the cloud platform provider.
8+
// ProviderName of the cloud platform provider
99
const ProviderName = "onprem"
1010

11-
// OnPrem provider for ops
11+
// OnPrem Provider to interact with OnPrem infrastructure
1212
type OnPrem struct{}
1313

14+
// NewProvider OnPrem
15+
func NewProvider() *OnPrem {
16+
return &OnPrem{}
17+
}
18+
1419
// Initialize on prem provider
1520
func (p *OnPrem) Initialize(config *types.ProviderConfig) error {
1621
return nil

provider/onprem/onprem_volume_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
Size: "",
3131
Path: "",
3232
}
33-
testOP = &onprem.OnPrem{}
33+
testOP = onprem.NewProvider()
3434
)
3535

3636
func NewTestContext(c *types.Config) *lepton.Context {

provider/openshift/provider.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import (
88
"github.com/nanovms/ops/types"
99
)
1010

11-
// Openshift provides access to the Openshift/Kubernetes API and implements the provider interface.
12-
type Openshift struct {
11+
// ProviderName of the cloud platform provider
12+
const ProviderName = "openshift"
13+
14+
// OpenShift provides access to the OpenShift/Kubernetes API and implements the provider interface.
15+
type OpenShift struct {
1316
client *Client
1417
}
1518

16-
// NewProvider returns an instance of Openshift provider
17-
func NewProvider() *Openshift {
18-
19-
return &Openshift{}
19+
// NewProvider Openshift
20+
func NewProvider() *OpenShift {
21+
return &OpenShift{}
2022
}
2123

2224
// Initialize prepares the openshift client and checks if the openshift server is up
23-
func (oc *Openshift) Initialize(config *types.ProviderConfig) error {
25+
func (oc *OpenShift) Initialize(config *types.ProviderConfig) error {
2426
client, err := New()
2527
if err != nil {
2628
return err
@@ -38,116 +40,116 @@ func (oc *Openshift) Initialize(config *types.ProviderConfig) error {
3840
}
3941

4042
// BuildImage builds the image
41-
func (oc *Openshift) BuildImage(ctx *lepton.Context) (string, error) {
43+
func (oc *OpenShift) BuildImage(ctx *lepton.Context) (string, error) {
4244
return "", nil
4345
}
4446

4547
// BuildImageWithPackage builds the image with package
46-
func (oc *Openshift) BuildImageWithPackage(ctx *lepton.Context, pkgpath string) (string, error) {
48+
func (oc *OpenShift) BuildImageWithPackage(ctx *lepton.Context, pkgpath string) (string, error) {
4749
return "", nil
4850
}
4951

5052
// CreateImage creates the image
51-
func (oc *Openshift) CreateImage(ctx *lepton.Context, imagePath string) error {
53+
func (oc *OpenShift) CreateImage(ctx *lepton.Context, imagePath string) error {
5254
return nil
5355
}
5456

5557
// ListImages lists all the images
56-
func (oc *Openshift) ListImages(ctx *lepton.Context) error {
58+
func (oc *OpenShift) ListImages(ctx *lepton.Context) error {
5759
return nil
5860
}
5961

6062
// GetImages gets all the images
61-
func (oc *Openshift) GetImages(ctx *lepton.Context) ([]lepton.CloudImage, error) {
63+
func (oc *OpenShift) GetImages(ctx *lepton.Context) ([]lepton.CloudImage, error) {
6264
return nil, nil
6365
}
6466

6567
// DeleteImage deletes the image using the image name
66-
func (oc *Openshift) DeleteImage(ctx *lepton.Context, imagename string) error {
68+
func (oc *OpenShift) DeleteImage(ctx *lepton.Context, imagename string) error {
6769
return nil
6870
}
6971

7072
// ResizeImage resizes the image
71-
func (oc *Openshift) ResizeImage(ctx *lepton.Context, imagename string, hbytes string) error {
73+
func (oc *OpenShift) ResizeImage(ctx *lepton.Context, imagename string, hbytes string) error {
7274
return nil
7375
}
7476

7577
// SyncImage sync an image from one provider to another
76-
func (oc *Openshift) SyncImage(config *types.Config, target lepton.Provider, imagename string) error {
78+
func (oc *OpenShift) SyncImage(config *types.Config, target lepton.Provider, imagename string) error {
7779
return nil
7880
}
7981

8082
// CustomizeImage customizes the images
81-
func (oc *Openshift) CustomizeImage(ctx *lepton.Context) (string, error) {
83+
func (oc *OpenShift) CustomizeImage(ctx *lepton.Context) (string, error) {
8284
return "", nil
8385
}
8486

8587
// CreateInstance creates nano instance
86-
func (oc *Openshift) CreateInstance(ctx *lepton.Context) error {
88+
func (oc *OpenShift) CreateInstance(ctx *lepton.Context) error {
8789
return nil
8890
}
8991

9092
// ListInstances lists all nano instances locally or on the provider
91-
func (oc *Openshift) ListInstances(ctx *lepton.Context) error {
93+
func (oc *OpenShift) ListInstances(ctx *lepton.Context) error {
9294
return nil
9395
}
9496

9597
// GetInstances gets all nano instances
96-
func (oc *Openshift) GetInstances(ctx *lepton.Context) ([]lepton.CloudInstance, error) {
98+
func (oc *OpenShift) GetInstances(ctx *lepton.Context) ([]lepton.CloudInstance, error) {
9799
return nil, nil
98100
}
99101

100102
// GetInstanceByName gets a nano instance by name
101-
func (oc *Openshift) GetInstanceByName(ctx *lepton.Context, name string) (*lepton.CloudInstance, error) {
103+
func (oc *OpenShift) GetInstanceByName(ctx *lepton.Context, name string) (*lepton.CloudInstance, error) {
102104
return nil, nil
103105
}
104106

105107
// DeleteInstance deletes a nano instance using the name
106-
func (oc *Openshift) DeleteInstance(ctx *lepton.Context, instancename string) error {
108+
func (oc *OpenShift) DeleteInstance(ctx *lepton.Context, instancename string) error {
107109
return nil
108110
}
109111

110112
// StopInstance stops a nanos instance
111-
func (oc *Openshift) StopInstance(ctx *lepton.Context, instancename string) error {
113+
func (oc *OpenShift) StopInstance(ctx *lepton.Context, instancename string) error {
112114
return nil
113115
}
114116

115117
// StartInstance starts a nanos instance
116-
func (oc *Openshift) StartInstance(ctx *lepton.Context, instancename string) error {
118+
func (oc *OpenShift) StartInstance(ctx *lepton.Context, instancename string) error {
117119
return nil
118120
}
119121

120122
// GetInstanceLogs gets the logs from a nanos instance
121-
func (oc *Openshift) GetInstanceLogs(ctx *lepton.Context, instancename string) (string, error) {
123+
func (oc *OpenShift) GetInstanceLogs(ctx *lepton.Context, instancename string) (string, error) {
122124
return "", nil
123125
}
124126

125127
// PrintInstanceLogs prints the logs from a nanos instance
126-
func (oc *Openshift) PrintInstanceLogs(ctx *lepton.Context, instancename string, watch bool) error {
128+
func (oc *OpenShift) PrintInstanceLogs(ctx *lepton.Context, instancename string, watch bool) error {
127129
return nil
128130
}
129131

130132
// CreateVolume creates a volume on the openshift cluster
131-
func (oc *Openshift) CreateVolume(ctx *lepton.Context, volumeName, data, provider string) (lepton.NanosVolume, error) {
133+
func (oc *OpenShift) CreateVolume(ctx *lepton.Context, volumeName, data, provider string) (lepton.NanosVolume, error) {
132134
return lepton.NanosVolume{}, nil
133135
}
134136

135137
// GetAllVolumes gets all volumes
136-
func (oc *Openshift) GetAllVolumes(ctx *lepton.Context) (*[]lepton.NanosVolume, error) {
138+
func (oc *OpenShift) GetAllVolumes(ctx *lepton.Context) (*[]lepton.NanosVolume, error) {
137139
return nil, nil
138140
}
139141

140142
// DeleteVolume deletes a volume
141-
func (oc *Openshift) DeleteVolume(ctx *lepton.Context, volumeName string) error {
143+
func (oc *OpenShift) DeleteVolume(ctx *lepton.Context, volumeName string) error {
142144
return nil
143145
}
144146

145147
// AttachVolume attaches a volume to a nano instance
146-
func (oc *Openshift) AttachVolume(ctx *lepton.Context, instanceName, volumeName string, attachID int) error {
148+
func (oc *OpenShift) AttachVolume(ctx *lepton.Context, instanceName, volumeName string, attachID int) error {
147149
return nil
148150
}
149151

150152
// DetachVolume detaches a volume from a nano instance
151-
func (oc *Openshift) DetachVolume(ctx *lepton.Context, instanceName, volumeName string) error {
153+
func (oc *OpenShift) DetachVolume(ctx *lepton.Context, instanceName, volumeName string) error {
152154
return nil
153155
}

0 commit comments

Comments
 (0)