forked from Telmate/proxmox-api-go
/
config_acme_account.go
80 lines (66 loc) · 2.1 KB
/
config_acme_account.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
73
74
75
76
77
78
79
80
package proxmox
import (
"encoding/json"
"strings"
"errors"
"fmt"
"log"
)
// Acme Account options for the Proxmox API
type ConfigAcmeAccount struct {
Name string `json:"name"`
Contact []string `json:"contact"`
Directory string `json:"directory"`
Tos bool `json:"tos,omitempty"`
}
func (config ConfigAcmeAccount) CreateAcmeAccount(acmeid string, client *Client) (err error) {
params := map[string]interface{}{
"name": acmeid,
"contact": ArrayToCSV(config.Contact),
}
if config.Tos == false{
return errors.New("error creating Acme account: the terms of service must be accepted")
}
acmeDirectories, err := client.GetAcmeDirectoriesUrl()
var tos string
if inArray(acmeDirectories ,config.Directory){
tos, err = client.GetAcmeTosUrl()
} else {
tos = "true"
}
params["tos_url"]=tos
params["directory"]=config.Directory
exitStatus, err := client.CreateAcmeAccount(params)
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error creating Acme profile: %v, error status: %s (params: %v)", err, exitStatus, string(params))
}
return
}
func NewConfigAcmeAccountFromApi(id string, client *Client) (config *ConfigAcmeAccount, err error) {
// prepare json map to receive the information from the api
var acmeConfig map[string]interface{}
acmeConfig, err = client.GetAcmeAccountConfig(id)
if err != nil {return nil, err}
config = new(ConfigAcmeAccount)
config.Name = id
config.Directory = acmeConfig["directory"].(string)
// Using the proxmox cli you can make the "tos" key empty
if acmeConfig["tos"] != nil {
if acmeConfig["tos"].(string) != ""{
config.Tos = true
}
}
contactArray := ArrayToStringType(acmeConfig["account"].(map[string]interface{})["contact"].([]interface{}))
config.Contact = make([]string, len(contactArray))
for i, element := range contactArray{
config.Contact[i] = strings.TrimPrefix(element, "mailto:")
}
return
}
func NewConfigAcmeAccountFromJson(input []byte) (config *ConfigAcmeAccount, err error) {
config = &ConfigAcmeAccount{}
err = json.Unmarshal([]byte(input), config)
if err != nil {log.Fatal(err)}
return
}