forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgradeapiloader.go
72 lines (62 loc) · 2.65 KB
/
upgradeapiloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package api
import (
"encoding/json"
"io/ioutil"
"github.com/Azure/acs-engine/pkg/api/vlabs"
"github.com/Azure/acs-engine/pkg/i18n"
)
// UpgradeApiloader represents the object that loads api model
type UpgradeApiloader struct {
Translator *i18n.Translator
}
// LoadUpgradeContainerServiceFromFile loads an ACS Cluster API Model from a JSON file
func (ua *UpgradeApiloader) LoadUpgradeContainerServiceFromFile(jsonFile string) (*UpgradeContainerService, string, error) {
contents, e := ioutil.ReadFile(jsonFile)
if e != nil {
return nil, "", ua.Translator.Errorf("error reading file %s: %s", jsonFile, e.Error())
}
return ua.DeserializeUpgradeContainerService(contents)
}
// DeserializeUpgradeContainerService loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (ua *UpgradeApiloader) DeserializeUpgradeContainerService(contents []byte) (*UpgradeContainerService, string, error) {
m := &TypeMeta{}
if err := json.Unmarshal(contents, &m); err != nil {
return nil, "", err
}
version := m.APIVersion
upgradecontainerservice, err := ua.LoadUpgradeContainerService(contents, version)
return upgradecontainerservice, version, err
}
// LoadUpgradeContainerService loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (ua *UpgradeApiloader) LoadUpgradeContainerService(contents []byte, version string) (*UpgradeContainerService, error) {
switch version {
case vlabs.APIVersion:
upgradecontainerService := &vlabs.UpgradeContainerService{}
if e := json.Unmarshal(contents, &upgradecontainerService); e != nil {
return nil, e
}
if e := upgradecontainerService.Validate(); e != nil {
return nil, e
}
return ConvertVLabsUpgradeContainerService(upgradecontainerService), nil
default:
return nil, ua.Translator.Errorf("unrecognized APIVersion '%s'", version)
}
}
// SerializeUpgradeContainerService takes an unversioned container service and returns the bytes
func (ua *UpgradeApiloader) SerializeUpgradeContainerService(upgradeContainerService *UpgradeContainerService, version string) ([]byte, error) {
switch version {
case vlabs.APIVersion:
vlabsUpgradeContainerService := ConvertUpgradeContainerServiceToVLabs(upgradeContainerService)
armUpgradeContainerService := &VlabsUpgradeContainerService{}
armUpgradeContainerService.UpgradeContainerService = vlabsUpgradeContainerService
armUpgradeContainerService.APIVersion = version
b, err := json.MarshalIndent(armUpgradeContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
default:
return nil, ua.Translator.Errorf("invalid version %s for conversion back from unversioned object", version)
}
}