Skip to content

Commit

Permalink
Changes cloud references to plain structs.
Browse files Browse the repository at this point in the history
There is no particular need to use pointers here. Simply using a value
instead prevents panics for poorly formed YAML for clouds, because we
can rely on zero values instead of getting nil-ref panics.

As a drive-by, we drop use of the the deprecated ioutil package.
  • Loading branch information
manadart committed Nov 20, 2023
1 parent fbe0680 commit 6e85fbe
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions cloud/clouds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cloud

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -241,7 +240,7 @@ func (r Region) IsEmpty() bool {
// unmarshalling.
type cloudSet struct {
// Clouds is a map of cloud definitions, keyed on cloud name.
Clouds map[string]*cloud `yaml:"clouds"`
Clouds map[string]cloud `yaml:"clouds"`
}

// cloud is equivalent to Cloud, for marshalling and unmarshalling.
Expand Down Expand Up @@ -369,7 +368,7 @@ func JujuPublicCloudsPath() string {
// are found, returns the fallback public cloud metadata.
func PublicCloudMetadata(searchPath ...string) (result map[string]Cloud, fallbackUsed bool, err error) {
for _, file := range searchPath {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil && os.IsNotExist(err) {
continue
}
Expand All @@ -388,7 +387,7 @@ func PublicCloudMetadata(searchPath ...string) (result map[string]Cloud, fallbac

// ParseOneCloud parses the given yaml bytes into a single Cloud metadata.
func ParseOneCloud(data []byte) (Cloud, error) {
c := &cloud{}
var c cloud
if err := yaml.Unmarshal(data, &c); err != nil {
return Cloud{}, errors.Annotate(err, "cannot unmarshal yaml cloud metadata")
}
Expand Down Expand Up @@ -444,7 +443,7 @@ func ParseCloudMetadata(data []byte) (map[string]Cloud, error) {
}
} else {
// Unable to coerce cloudSet, try to unmarshal into a map[string]*cloud
cloudMap := make(map[string]*cloud)
cloudMap := make(map[string]cloud)
if errCloudMap := yaml.Unmarshal(data, &cloudMap); errCloudMap != nil {
return nil, errors.Errorf("Invalid cloud metadata %s", yamlMap)
}
Expand Down Expand Up @@ -515,7 +514,7 @@ func IsSameCloudMetadata(meta1, meta2 map[string]Cloud) (bool, error) {

// marshalCloudMetadata marshals the given clouds to YAML.
func marshalCloudMetadata(cloudsMap map[string]Cloud) ([]byte, error) {
clouds := cloudSet{make(map[string]*cloud)}
clouds := cloudSet{make(map[string]cloud)}
for name, metadata := range cloudsMap {
clouds.Clouds[name] = cloudToInternal(metadata, false)
}
Expand All @@ -537,10 +536,10 @@ func UnmarshalCloud(in []byte) (Cloud, error) {
if err := yaml.Unmarshal(in, &internal); err != nil {
return Cloud{}, errors.Annotate(err, "cannot unmarshal yaml cloud metadata")
}
return cloudFromInternal(&internal), nil
return cloudFromInternal(internal), nil
}

func cloudToInternal(in Cloud, withName bool) *cloud {
func cloudToInternal(in Cloud, withName bool) cloud {
var regions regions
for _, r := range in.Regions {
regions.Slice = append(regions.Slice, yaml.MapItem{
Expand All @@ -556,7 +555,7 @@ func cloudToInternal(in Cloud, withName bool) *cloud {
if !withName {
name = ""
}
return &cloud{
return cloud{
Name: name,
Type: in.Type,
HostCloudRegion: in.HostCloudRegion,
Expand All @@ -573,7 +572,7 @@ func cloudToInternal(in Cloud, withName bool) *cloud {
}
}

func cloudFromInternal(in *cloud) Cloud {
func cloudFromInternal(in cloud) Cloud {
var regions []Region
if len(in.Regions.Map) > 0 {
for _, item := range in.Regions.Slice {
Expand Down

0 comments on commit 6e85fbe

Please sign in to comment.