This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
/
resource_foreman_operatingsystem.go
232 lines (194 loc) · 6.32 KB
/
resource_foreman_operatingsystem.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package foreman
import (
"fmt"
"strconv"
"github.com/wayfair/terraform-provider-foreman/foreman/api"
"github.com/wayfair/terraform-provider-utils/autodoc"
"github.com/wayfair/terraform-provider-utils/log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceForemanOperatingSystem() *schema.Resource {
return &schema.Resource{
Create: resourceForemanOperatingSystemCreate,
Read: resourceForemanOperatingSystemRead,
Update: resourceForemanOperatingSystemUpdate,
Delete: resourceForemanOperatingSystemDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
autodoc.MetaAttribute: &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: fmt.Sprintf(
"%s Foreman representation of an operating system.",
autodoc.MetaSummary,
),
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: fmt.Sprintf(
"Operating system name. "+
"%s \"CentOS\"",
autodoc.MetaExample,
),
},
"major": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: fmt.Sprintf(
"Major release version. "+
"%s \"7\"",
autodoc.MetaExample,
),
},
"minor": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf(
"Minor release version. "+
"%s \"4\"",
autodoc.MetaExample,
),
},
"title": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The operating system's title is a concatentation of " +
"the OS name, major, and minor versions to get a full operating " +
"system release.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Additional operating system information.",
},
"family": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"AIX",
"Altlinux",
"Archlinux",
"Coreos",
"Debian",
"Freebsd",
"Gentoo",
"Junos",
"NXOS",
"Redhat",
"Solaris",
"Suse",
"Windows",
// NOTE(ALL): false - do not ignore case when comparing values
}, false),
Description: "Operating system family. Values include: " +
"`\"AIX\"`, `\"Altlinux\"`, `\"Archlinux\"`, `\"Coreos\"`, " +
"`\"Debian\"`, `\"Freebsd\"`, `\"Gentoo\"`, `\"Junos\"`, " +
"`\"NXOS\"`, `\"Redhat\"`, `\"Solaris\"`, `\"Suse\"`, `\"Windows\"`.",
},
"release_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Code name or release name for the specific operating " +
"system version.",
},
"password_hash": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"MD5",
"SHA256",
"SHA512",
"Base64",
// NOTE(ALL): false - do not ignore case when comparing values
}, false),
Description: "Root password hash function to use. Valid values " +
"include: `\"MD5\"`, `\"SHA256\"`, `\"SHA512\"`, `\"Base64\"`.",
},
},
}
}
// -----------------------------------------------------------------------------
// Conversion Helpers
// -----------------------------------------------------------------------------
// buildForemanOperatingSystem constructs a ForemanOperatingSystem reference
// from a resource data reference. The struct's members are populated from
// the data populated in the resource data. Missing members will be left to
// the zero value for that member's type.
func buildForemanOperatingSystem(d *schema.ResourceData) *api.ForemanOperatingSystem {
log.Tracef("resource_foreman_operatingsystem.go#buildForemanOperatingSystem")
os := api.ForemanOperatingSystem{}
obj := buildForemanObject(d)
os.ForemanObject = *obj
var attr interface{}
var ok bool
if attr, ok = d.GetOk("major"); ok {
os.Major = attr.(string)
}
if attr, ok = d.GetOk("minor"); ok {
os.Minor = attr.(string)
}
if attr, ok = d.GetOk("title"); ok {
os.Title = attr.(string)
}
if attr, ok = d.GetOk("description"); ok {
os.Description = attr.(string)
}
if attr, ok = d.GetOk("family"); ok {
os.Family = attr.(string)
}
if attr, ok = d.GetOk("release_name"); ok {
os.ReleaseName = attr.(string)
}
if attr, ok = d.GetOk("password_hash"); ok {
os.PasswordHash = attr.(string)
}
return &os
}
// setResourceDataFromOperatingSystem sets a ResourceData's attributes from the
// attributes of the supplied ForemanOperatingSystem reference
func setResourceDataFromForemanOperatingSystem(d *schema.ResourceData, fo *api.ForemanOperatingSystem) {
log.Tracef("resource_foreman_operatingsystem.go#setResourceDataFromForemanOperatingSystem")
d.SetId(strconv.Itoa(fo.Id))
d.Set("name", fo.Name)
d.Set("major", fo.Major)
d.Set("minor", fo.Minor)
d.Set("title", fo.Title)
d.Set("description", fo.Description)
d.Set("family", fo.Family)
d.Set("release_name", fo.ReleaseName)
d.Set("password_hash", fo.PasswordHash)
}
// -----------------------------------------------------------------------------
// Resource CRUD Operations
// -----------------------------------------------------------------------------
func resourceForemanOperatingSystemCreate(d *schema.ResourceData, meta interface{}) error {
log.Tracef("resource_foreman_operatingsystem.go#Create")
return nil
}
func resourceForemanOperatingSystemRead(d *schema.ResourceData, meta interface{}) error {
log.Tracef("resource_foreman_operatingsystem.go#Read")
client := meta.(*api.Client)
o := buildForemanOperatingSystem(d)
log.Debugf("ForemanOperatingSystem: [%+v]", o)
readOS, readErr := client.ReadOperatingSystem(o.Id)
if readErr != nil {
return readErr
}
log.Debugf("ForemanOperatingSystem: [%+v]", readOS)
setResourceDataFromForemanOperatingSystem(d, readOS)
return nil
}
func resourceForemanOperatingSystemUpdate(d *schema.ResourceData, meta interface{}) error {
log.Tracef("resource_foreman_operatingsystem.go#Update")
return nil
}
func resourceForemanOperatingSystemDelete(d *schema.ResourceData, meta interface{}) error {
log.Tracef("resource_foreman_operatingsystem.go#Delete")
// NOTE(ALL): d.SetId("") is automatically called by terraform assuming delete
// returns no errors
return nil
}