-
Notifications
You must be signed in to change notification settings - Fork 277
/
template.go
132 lines (116 loc) · 3.64 KB
/
template.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zmscli
import (
"bytes"
"fmt"
"strings"
"github.com/AthenZ/athenz/clients/go/zms"
)
func (cli Zms) ListServerTemplates() (*string, error) {
templates, err := cli.Zms.GetServerTemplateList()
if err != nil {
return nil, err
}
oldYamlConverter := func(res interface{}) (*string, error) {
var buf bytes.Buffer
buf.WriteString("templates:\n")
for _, name := range templates.TemplateNames {
buf.WriteString(indentLevel1Dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
}
return cli.dumpByFormat(templates, oldYamlConverter)
}
func (cli Zms) ListDomainTemplates(dn string) (*string, error) {
templates, err := cli.Zms.GetDomainTemplateList(zms.DomainName(dn))
if err != nil {
return nil, err
}
oldYamlConverter := func(res interface{}) (*string, error) {
var buf bytes.Buffer
buf.WriteString("templates:\n")
for _, name := range templates.TemplateNames {
buf.WriteString(indentLevel1Dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
}
return cli.dumpByFormat(templates, oldYamlConverter)
}
func (cli Zms) ShowServerTemplate(templateName string) (*string, error) {
template, err := cli.Zms.GetTemplate(zms.SimpleName(templateName))
if err != nil {
return nil, err
}
oldYamlConverter := func(res interface{}) (*string, error) {
var buf bytes.Buffer
buf.WriteString("template:\n")
cli.dumpMetadata(&buf, template.Metadata, indentLevel2Dash, templateName)
buf.WriteString(indentLevel1 + "roles:\n")
for _, role := range template.Roles {
cli.dumpRole(&buf, *role, false, indentLevel2Dash, indentLevel2DashLvl)
}
buf.WriteString(indentLevel1 + "policies:\n")
for _, policy := range template.Policies {
cli.dumpPolicy(&buf, *policy, indentLevel2Dash, indentLevel2DashLvl)
}
if len(template.Services) > 0 {
buf.WriteString(indentLevel1 + "services:\n")
for _, service := range template.Services {
cli.dumpService(&buf, *service, indentLevel2Dash, indentLevel2DashLvl)
}
}
s := buf.String()
return &s, nil
}
return cli.dumpByFormat(template, oldYamlConverter)
}
func (cli Zms) SetDomainTemplate(dn string, templateArgs []string) (*string, error) {
templateNames := make([]zms.SimpleName, 0)
templateParams := make([]*zms.TemplateParam, 0)
for _, value := range templateArgs {
idx := strings.Index(value, "=")
if idx < 0 {
templateNames = append(templateNames, zms.SimpleName(value))
} else {
param := zms.TemplateParam{
Name: zms.SimpleName(value[0:idx]),
Value: zms.CompoundName(value[idx+1:]),
}
templateParams = append(templateParams, ¶m)
}
}
//make sure we have some templates specified
if len(templateNames) == 0 {
return nil, fmt.Errorf("no template names specified")
}
var domainTemplateList zms.DomainTemplate
domainTemplateList.TemplateNames = templateNames
if len(templateParams) > 0 {
domainTemplateList.Params = templateParams
}
err := cli.Zms.PutDomainTemplate(zms.DomainName(dn), cli.AuditRef, &domainTemplateList)
if err != nil {
return nil, err
}
s := "[Template(s) successfully applied to domain]"
message := SuccessMessage{
Status: 200,
Message: s,
}
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}
func (cli Zms) DeleteDomainTemplate(dn string, template string) (*string, error) {
err := cli.Zms.DeleteDomainTemplate(zms.DomainName(dn), zms.SimpleName(template), cli.AuditRef)
if err != nil {
return nil, err
}
s := "[Deleted template: " + template + "]"
message := SuccessMessage{
Status: 200,
Message: s,
}
return cli.dumpByFormat(message, cli.buildYAMLOutput)
}