-
Notifications
You must be signed in to change notification settings - Fork 276
/
template.go
101 lines (92 loc) · 2.79 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
// 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/yahoo/athenz/clients/go/zms"
)
func (cli Zms) ListServerTemplates() (*string, error) {
var buf bytes.Buffer
templates, err := cli.Zms.GetServerTemplateList()
if err != nil {
return nil, err
}
buf.WriteString("templates:\n")
for _, name := range templates.TemplateNames {
buf.WriteString(indent_level1_dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
}
func (cli Zms) ListDomainTemplates(dn string) (*string, error) {
var buf bytes.Buffer
templates, err := cli.Zms.GetDomainTemplateList(zms.DomainName(dn))
if err != nil {
return nil, err
}
buf.WriteString("templates:\n")
for _, name := range templates.TemplateNames {
buf.WriteString(indent_level1_dash + string(name) + "\n")
}
s := buf.String()
return &s, nil
}
func (cli Zms) ShowServerTemplate(templateName string) (*string, error) {
template, err := cli.Zms.GetTemplate(zms.SimpleName(templateName))
if err != nil {
return nil, err
}
var buf bytes.Buffer
buf.WriteString("template:\n")
buf.WriteString(indent_level1 + "roles:\n")
for _, role := range template.Roles {
cli.dumpRole(&buf, *role, false, indent_level2_dash, indent_level2_dash_lvl)
}
buf.WriteString(indent_level1 + "policies:\n")
for _, policy := range template.Policies {
cli.dumpPolicy(&buf, *policy, indent_level2_dash, indent_level2_dash_lvl)
}
s := buf.String()
return &s, nil
}
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.SimpleName(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]"
return &s, nil
}
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 + "]"
return &s, nil
}