-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_parameter.go
168 lines (157 loc) · 5.08 KB
/
resource_parameter.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/umweltdk/teamcity/types"
"log"
)
func resourceParameter() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"label": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
// Text type options
"validation_mode": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
// Checkbox type options
"checked_value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"unchecked_value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
// Select options
"allow_multiple": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"value_separator": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"items": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
},
}
}
func parameterValueHash(v interface{}) int {
rd := v.(map[string]interface{})
name := rd["name"].(string)
spec := definitionToParameterSpec(rd)
/*
var rawType string
if spec != nil {
rawType = spec.String()
}
*/
hk := fmt.Sprintf("%s=%s", name, spec)
//fmt.Printf("[DEBUG] TeamCity parameterValueHash(%#v): %s: hk=%s,hc=%d\n", v, name, hk, hashcode.String(hk))
log.Printf("[DEBUG] TeamCity parameterValueHash(%#v): %s: hk=%s,hc=%d\n", v, name, hk, hashcode.String(hk))
return hashcode.String(hk)
}
func parameterKeyHash(v interface{}) int {
m := v.(map[string]interface{})
hk := m["name"].(string)
//fmt.Printf("[DEBUG] TeamCity parameterKeyHash(%#v): %s: hk=%s,hc=%d\n", v, hk, hk, hashcode.String(hk))
log.Printf("[DEBUG] TeamCity parameterKeyHash(%#v): %s: hk=%s,hc=%d\n", v, hk, hk, hashcode.String(hk))
return hashcode.String(hk)
}
func parametersToDefinition(parameters types.Parameters) *schema.Set {
ret := schema.NewSet(parameterValueHash, []interface{}{})
for name, parameter := range parameters {
param := make(map[string]interface{})
if parameter.Spec != nil {
spec := *parameter.Spec
param["label"] = spec.Label
param["description"] = spec.Description
typeName := spec.Type.TypeName()
param["type"] = typeName
if typeName == "text" {
param["validation_mode"] = spec.Type.(types.TextType).ValidationMode
} else if typeName == "checkbox" {
param["checked_value"] = spec.Type.(types.CheckboxType).Checked
param["unchecked_value"] = spec.Type.(types.CheckboxType).Unchecked
} else if typeName == "select" {
param["allow_multiple"] = spec.Type.(types.SelectType).AllowMultiple
param["value_separator"] = spec.Type.(types.SelectType).ValueSeparator
}
}
param["name"] = name
log.Printf("[INFO] Parameter %q\n", param)
ret.Add(param)
}
return ret
}
func parameterValues(parameters types.Parameters) map[string]interface{} {
ret := make(map[string]interface{})
for name, parameter := range parameters {
ret[name] = parameter.Value
}
return ret
}
func definitionToParameterSpec(param map[string]interface{}) *types.ParameterSpec {
if param["type"].(string) != "" || param["label"].(string) != "" || param["description"].(string) != "" {
var tp types.ParameterType
if param["type"].(string) == "text" {
tp = &types.TextType{
ValidationMode: param["validation_mode"].(string),
}
} else if param["type"].(string) == "password" {
tp = &types.PasswordType{}
} else if param["type"].(string) == "checkbox" {
tp = &types.CheckboxType{
Checked: param["checked_value"].(string),
Unchecked: param["unchecked_value"].(string),
}
} else if param["type"].(string) == "select" {
tp = &types.SelectType{
AllowMultiple: param["allow_multiple"].(bool),
ValueSeparator: param["value_separator"].(string),
}
} else {
tp = &types.TextType{"any"}
}
ret := &types.ParameterSpec{
Label: param["label"].(string),
Description: param["description"].(string),
Type: tp,
}
log.Printf("Parameter %s => %q", param["name"].(string), ret)
return ret
}
return nil
}
func definitionToParameters(parameters schema.Set) types.Parameters {
keySet := schema.NewSet(parameterKeyHash, parameters.List())
ret := make(types.Parameters)
for _, v := range keySet.List() {
param := v.(map[string]interface{})
parameter := types.Parameter{
Spec: definitionToParameterSpec(param),
}
ret[param["name"].(string)] = parameter
}
return ret
}