forked from rancher/go-machine-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.go
62 lines (53 loc) · 1.25 KB
/
azure.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
package providers
import (
b64 "encoding/base64"
"os"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/rancher/go-rancher/client"
)
func init() {
azureHandler := &AzureHandler{}
if err := RegisterProvider("azure", azureHandler); err != nil {
log.Fatal("could not register azure provider")
}
}
type AzureHandler struct {
}
func (*AzureHandler) HandleCreate(machine *client.Machine, machineDir string) error {
var data *string
var filename string
if machine.AzureConfig.SubscriptionCert != "" {
data = &machine.AzureConfig.SubscriptionCert
filename = "subscription-cert.pem"
} else {
data = &machine.AzureConfig.PublishSettingsFile
filename = "publish-settings.xml"
}
path, err := saveDataToFile(filename, *data, machineDir)
if err != nil {
return err
}
*data = path
return nil
}
func (*AzureHandler) HandleError(msg string) string {
return msg
}
func saveDataToFile(filename, data, machineDir string) (string, error) {
f, err := os.Create(filepath.Join(machineDir, filename))
defer f.Close()
if err != nil {
return "", err
}
var byteData []byte
byteData, err = b64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
_, err = f.Write(byteData)
if err != nil {
return "", err
}
return f.Name(), nil
}