2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cloudprovider
|
|
|
|
|
2022-10-11 06:24:33 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
2022-05-02 03:28:37 -04:00
|
|
|
//go:generate stringer -type=Provider
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 09:09:33 -04:00
|
|
|
// Provider is cloud provider used by the CLI.
|
|
|
|
type Provider uint32
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
const (
|
2022-11-09 09:57:54 -05:00
|
|
|
// Unknown is default value for Provider.
|
2022-04-13 09:09:33 -04:00
|
|
|
Unknown Provider = iota
|
2022-11-09 09:57:54 -05:00
|
|
|
// AWS is Amazon Web Services.
|
2022-03-22 11:03:15 -04:00
|
|
|
AWS
|
2022-11-09 09:57:54 -05:00
|
|
|
// Azure cloud.
|
2022-03-22 11:03:15 -04:00
|
|
|
Azure
|
2022-11-09 09:57:54 -05:00
|
|
|
// GCP is Google Compute Platform.
|
2022-03-22 11:03:15 -04:00
|
|
|
GCP
|
2023-02-27 12:19:52 -05:00
|
|
|
// OpenStack is an open standard cloud computing platform.
|
|
|
|
OpenStack
|
2022-11-09 09:57:54 -05:00
|
|
|
// QEMU for a local emulated installation.
|
2022-05-02 03:28:37 -04:00
|
|
|
QEMU
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
2022-04-13 07:01:38 -04:00
|
|
|
|
2022-10-11 06:24:33 -04:00
|
|
|
// MarshalJSON marshals the Provider to JSON string.
|
|
|
|
func (p Provider) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(p.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the Provider from JSON string.
|
|
|
|
func (p *Provider) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*p = FromString(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 04:27:33 -05:00
|
|
|
// MarshalYAML marshals the Provider to YAML string.
|
2023-03-08 04:31:20 -05:00
|
|
|
func (p Provider) MarshalYAML() (any, error) {
|
2022-11-28 04:27:33 -05:00
|
|
|
return p.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals the Provider from YAML string.
|
|
|
|
func (p *Provider) UnmarshalYAML(unmarshal func(any) error) error {
|
|
|
|
var s string
|
|
|
|
if err := unmarshal(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*p = FromString(s)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
// FromString returns cloud provider from string.
|
2022-04-13 09:09:33 -04:00
|
|
|
func FromString(s string) Provider {
|
2022-04-13 07:01:38 -04:00
|
|
|
s = strings.ToLower(s)
|
2023-05-30 03:02:50 -04:00
|
|
|
if isOpenStackProvider(s) {
|
|
|
|
return OpenStack
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
switch s {
|
|
|
|
case "aws":
|
|
|
|
return AWS
|
|
|
|
case "azure":
|
|
|
|
return Azure
|
|
|
|
case "gcp":
|
|
|
|
return GCP
|
2022-05-02 03:28:37 -04:00
|
|
|
case "qemu":
|
|
|
|
return QEMU
|
2022-04-13 07:01:38 -04:00
|
|
|
default:
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 03:02:50 -04:00
|
|
|
|
|
|
|
// IsOpenStackProvider returns true if the provider is based on OpenStack.
|
|
|
|
func isOpenStackProvider(s string) bool {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "openstack", "stackit":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|