Skip to content

Commit 4a410d1

Browse files
authored
Puppet fixes (#111)
* Add config_group support to hosts and hostgroups * Update documentation
1 parent e5a4544 commit 4a410d1

File tree

8 files changed

+65
-0
lines changed

8 files changed

+65
-0
lines changed

docs/data-sources/foreman_hostgroup.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following attributes are exported:
2828

2929
- `architecture_id` - ID of the architecture associated with this hostgroup.
3030
- `compute_profile_id` - ID of the compute profile associated with this hostgroup.
31+
- `config_group_ids` - IDs of the applied config groups.
3132
- `content_source_id` - ID of the content source associated with this hostgroup.
3233
- `content_view_id` - ID of the content view associated with this hostgroup.
3334
- `domain_id` - ID of the domain associated with this hostgroup.

docs/resources/foreman_host.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The following arguments are supported:
2525
- `compute_attributes` - (Optional) Hypervisor specific VM options. Must be a JSON string, as every compute provider has different attributes schema
2626
- `compute_profile_id` - (Optional)
2727
- `compute_resource_id` - (Optional, Force New)
28+
- `config_group_ids` - (Optional) IDs of the applied config groups.
2829
- `domain_id` - (Optional, Force New) ID of the domain to assign to the host.
2930
- `enable_bmc` - (Optional) Enables PMI/BMC functionality. On create and update calls, having this enabled will force a host to poweroff, set next boot to PXE and power on. Defaults to `false`.
3031
- `environment_id` - (Optional) ID of the environment to assign to the host.
@@ -55,6 +56,7 @@ The following attributes are exported:
5556
- `compute_attributes` - Hypervisor specific VM options. Must be a JSON string, as every compute provider has different attributes schema
5657
- `compute_profile_id` -
5758
- `compute_resource_id` -
59+
- `config_group_ids` - IDs of the applied config groups.
5860
- `domain_id` - ID of the domain to assign to the host.
5961
- `domain_name` - The domain name of the host.
6062
- `enable_bmc` - Enables PMI/BMC functionality. On create and update calls, having this enabled will force a host to poweroff, set next boot to PXE and power on. Defaults to `false`.

docs/resources/foreman_hostgroup.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following arguments are supported:
2121

2222
- `architecture_id` - (Optional) ID of the architecture associated with this hostgroup.
2323
- `compute_profile_id` - (Optional) ID of the compute profile associated with this hostgroup.
24+
- `config_group_ids` - (Optional) IDs of the applied config groups.
2425
- `content_source_id` - (Optional) ID of the content source associated with this hostgroup.
2526
- `content_view_id` - (Optional) ID of the content view associated with this hostgroup.
2627
- `domain_id` - (Optional) ID of the domain associated with this hostgroup.
@@ -47,6 +48,7 @@ The following attributes are exported:
4748

4849
- `architecture_id` - ID of the architecture associated with this hostgroup.
4950
- `compute_profile_id` - ID of the compute profile associated with this hostgroup.
51+
- `config_group_ids` - IDs of the applied config groups.
5052
- `content_source_id` - ID of the content source associated with this hostgroup.
5153
- `content_view_id` - ID of the content view associated with this hostgroup.
5254
- `domain_id` - ID of the domain associated with this hostgroup.

foreman/api/client.go

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ type ForemanKVParameter struct {
101101
Value string `json:"value"`
102102
}
103103

104+
// JSON obect for creating and updating puppetattributes on hosts and hostgroups
105+
type PuppetAttribute struct {
106+
Puppetclass_ids []int `json:"puppetclass_ids"`
107+
ConfigGroup_ids []int `json:"config_group_ids"`
108+
}
109+
104110
func FromKV(kv []ForemanKVParameter) (ret map[string]string) {
105111
ret = make(map[string]string)
106112
for _, pair := range kv {
@@ -421,5 +427,6 @@ func (client *Client) WrapJSONWithTaxonomy(name interface{}, item interface{}) (
421427
wrapped["organization_id"] = client.clientConfig.OrganizationID
422428
log.Debugf("client.go#WrapJSONWithTaxonomy: item %+v", wrapped)
423429
}
430+
424431
return json.Marshal(wrapped)
425432
}

foreman/api/host.go

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ type ForemanHost struct {
9595
PuppetClassIds []int `json:"puppet_class_ids,omitempty"`
9696
// Build token
9797
Token string `json:"token,omitempty"`
98+
// List of config groups to apply to the hostg
99+
ConfigGroupIds []int `json:"config_group_ids"`
100+
// The puppetattributes object is only used for create and update, it's not populated on read, hence the duplication
101+
PuppetAttributes PuppetAttribute `json:"puppet_attributes"`
98102
}
99103

100104
// ForemanInterfacesAttribute representing a hosts defined network interfaces
@@ -138,6 +142,7 @@ type foremanHostDecode struct {
138142
ForemanHost
139143
InterfacesAttributesDecode []ForemanInterfacesAttribute `json:"interfaces"`
140144
PuppetClassesDecode []ForemanObject `json:"puppetclasses"`
145+
ConfigGroupsDecode []ForemanObject `json:"config_groups"`
141146
HostParametersDecode []ForemanKVParameter `json:"parameters"`
142147
}
143148

@@ -276,6 +281,7 @@ func (c *Client) CreateHost(ctx context.Context, h *ForemanHost, retryCount int)
276281

277282
createdHost.InterfacesAttributes = createdHost.InterfacesAttributesDecode
278283
createdHost.PuppetClassIds = foremanObjectArrayToIdIntArray(createdHost.PuppetClassesDecode)
284+
createdHost.ConfigGroupIds = foremanObjectArrayToIdIntArray(createdHost.ConfigGroupsDecode)
279285
createdHost.HostParameters = createdHost.HostParametersDecode
280286

281287
computeAttributes, _ := c.readComputeAttributes(ctx, createdHost.Id)
@@ -317,6 +323,7 @@ func (c *Client) ReadHost(ctx context.Context, id int) (*ForemanHost, error) {
317323
}
318324
readHost.InterfacesAttributes = readHost.InterfacesAttributesDecode
319325
readHost.PuppetClassIds = foremanObjectArrayToIdIntArray(readHost.PuppetClassesDecode)
326+
readHost.ConfigGroupIds = foremanObjectArrayToIdIntArray(readHost.ConfigGroupsDecode)
320327
readHost.HostParameters = readHost.HostParametersDecode
321328

322329
return &readHost.ForemanHost, nil
@@ -372,6 +379,7 @@ func (c *Client) UpdateHost(ctx context.Context, h *ForemanHost, retryCount int)
372379
}
373380
updatedHost.InterfacesAttributes = updatedHost.InterfacesAttributesDecode
374381
updatedHost.PuppetClassIds = foremanObjectArrayToIdIntArray(updatedHost.PuppetClassesDecode)
382+
updatedHost.ConfigGroupIds = foremanObjectArrayToIdIntArray(updatedHost.ConfigGroupsDecode)
375383
updatedHost.HostParameters = updatedHost.HostParametersDecode
376384
log.Debugf("updatedHost: [%+v]", updatedHost)
377385

foreman/api/hostgroup.go

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type ForemanHostgroup struct {
4141
ArchitectureId int `json:"architecture_id,omitempty"`
4242
// ID of the compute profile associated with this hostgroup
4343
ComputeProfileId int `json:"compute_profile_id,omitempty"`
44+
// List of config groups to apply to the hostgroup
45+
ConfigGroupIds []int `json:"config_group_ids"`
4446
// ID of the domain associated with this hostgroup
4547
DomainId int `json:"domain_id,omitempty"`
4648
// ID of the environment associated with this hostgroup
@@ -75,6 +77,8 @@ type ForemanHostgroup struct {
7577
ContentSourceId int `json:"content_source_id,omitempty"`
7678
// Map of HostGroupParameters
7779
HostGroupParameters []ForemanKVParameter `json:"group_parameters_attributes,omitempty"`
80+
// The puppetattributes object is only used for create and update, it's not populated on read, hence the duplication
81+
PuppetAttributes PuppetAttribute `json:"puppet_attributes"`
7882
}
7983

8084
// ForemanHostgroup struct used for JSON decode. Foreman API returns the ids
@@ -83,6 +87,7 @@ type ForemanHostgroup struct {
8387
type foremanHostGroupDecode struct {
8488
ForemanHostgroup
8589
PuppetClassesDecode []ForemanObject `json:"puppetclasses"`
90+
ConfigGroupsDecode []ForemanObject `json:"config_groups"`
8691
HostGroupParametersDecode []ForemanKVParameter `json:"parameters,omitempty"`
8792
}
8893

@@ -151,6 +156,7 @@ func (c *Client) ReadHostgroup(ctx context.Context, id int) (*ForemanHostgroup,
151156
}
152157

153158
readHostgroup.PuppetClassIds = foremanObjectArrayToIdIntArray(readHostgroup.PuppetClassesDecode)
159+
readHostgroup.ConfigGroupIds = foremanObjectArrayToIdIntArray(readHostgroup.ConfigGroupsDecode)
154160
readHostgroup.HostGroupParameters = readHostgroup.HostGroupParametersDecode
155161

156162
log.Debugf("readHostgroup: [%+v]", readHostgroup)
@@ -191,6 +197,7 @@ func (c *Client) UpdateHostgroup(ctx context.Context, h *ForemanHostgroup) (*For
191197
}
192198

193199
updatedHostgroup.PuppetClassIds = foremanObjectArrayToIdIntArray(updatedHostgroup.PuppetClassesDecode)
200+
updatedHostgroup.ConfigGroupIds = foremanObjectArrayToIdIntArray(updatedHostgroup.ConfigGroupsDecode)
194201
updatedHostgroup.HostGroupParameters = updatedHostgroup.HostGroupParametersDecode
195202

196203
log.Debugf("updatedHostgroup: [%+v]", updatedHostgroup)

foreman/resource_foreman_host.go

+20
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ func resourceForemanHost() *schema.Resource {
527527
},
528528
Description: "IDs of the applied puppet classes.",
529529
},
530+
"config_group_ids": {
531+
Type: schema.TypeSet,
532+
Optional: true,
533+
Computed: true,
534+
Elem: &schema.Schema{
535+
Type: schema.TypeInt,
536+
},
537+
Description: "IDs of the applied config groups.",
538+
},
530539
"compute_resource_id": {
531540
Type: schema.TypeInt,
532541
Optional: true,
@@ -775,7 +784,15 @@ func buildForemanHost(d *schema.ResourceData) *api.ForemanHost {
775784
if attr, ok = d.GetOk("puppet_class_ids"); ok {
776785
attrSet := attr.(*schema.Set)
777786
host.PuppetClassIds = conv.InterfaceSliceToIntSlice(attrSet.List())
787+
host.PuppetAttributes.Puppetclass_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
778788
}
789+
790+
if attr, ok = d.GetOk("config_group_ids"); ok {
791+
attrSet := attr.(*schema.Set)
792+
host.ConfigGroupIds = conv.InterfaceSliceToIntSlice(attrSet.List())
793+
host.PuppetAttributes.ConfigGroup_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
794+
}
795+
779796
if attr, ok = d.GetOk("parameters"); ok {
780797
host.HostParameters = api.ToKV(attr.(map[string]interface{}))
781798
}
@@ -951,6 +968,7 @@ func setResourceDataFromForemanHost(d *schema.ResourceData, fh *api.ForemanHost)
951968
d.Set("image_id", fh.ImageId)
952969
d.Set("model_id", fh.ModelId)
953970
d.Set("puppet_class_ids", fh.PuppetClassIds)
971+
d.Set("config_group_ids", fh.ConfigGroupIds)
954972
d.Set("token", fh.Token)
955973

956974
setResourceDataFromForemanInterfacesAttributes(d, fh)
@@ -1175,6 +1193,8 @@ func resourceForemanHostUpdate(ctx context.Context, d *schema.ResourceData, meta
11751193
d.HasChange("operatingsystem_id") ||
11761194
d.HasChange("interfaces_attributes") ||
11771195
d.HasChange("build") ||
1196+
d.HasChange("puppet_class_ids") ||
1197+
d.HasChange("config_group_ids") ||
11781198
d.Get("managed") == false {
11791199

11801200
log.Debugf("host: [%+v]", h)

foreman/resource_foreman_hostgroup.go

+18
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ func resourceForemanHostgroup() *schema.Resource {
109109
Description: "ID of the compute profile associated with this hostgroup.",
110110
},
111111

112+
"config_group_ids": {
113+
Type: schema.TypeSet,
114+
Optional: true,
115+
Computed: true,
116+
Elem: &schema.Schema{
117+
Type: schema.TypeInt,
118+
},
119+
Description: "IDs of the applied config groups.",
120+
},
121+
112122
"content_source_id": {
113123
Type: schema.TypeInt,
114124
Optional: true,
@@ -264,6 +274,12 @@ func buildForemanHostgroup(d *schema.ResourceData) *api.ForemanHostgroup {
264274
hostgroup.ComputeProfileId = attr.(int)
265275
}
266276

277+
if attr, ok = d.GetOk("config_group_ids"); ok {
278+
attrSet := attr.(*schema.Set)
279+
hostgroup.ConfigGroupIds = conv.InterfaceSliceToIntSlice(attrSet.List())
280+
hostgroup.PuppetAttributes.ConfigGroup_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
281+
}
282+
267283
if attr, ok = d.GetOk("content_source_id"); ok {
268284
hostgroup.ContentSourceId = attr.(int)
269285
}
@@ -307,6 +323,7 @@ func buildForemanHostgroup(d *schema.ResourceData) *api.ForemanHostgroup {
307323
if attr, ok = d.GetOk("puppet_class_ids"); ok {
308324
attrSet := attr.(*schema.Set)
309325
hostgroup.PuppetClassIds = conv.InterfaceSliceToIntSlice(attrSet.List())
326+
hostgroup.PuppetAttributes.Puppetclass_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
310327
}
311328

312329
if attr, ok = d.GetOk("puppet_proxy_id"); ok {
@@ -339,6 +356,7 @@ func setResourceDataFromForemanHostgroup(d *schema.ResourceData, fh *api.Foreman
339356
d.Set("parameters", api.FromKV(fh.HostGroupParameters))
340357
d.Set("architecture_id", fh.ArchitectureId)
341358
d.Set("compute_profile_id", fh.ComputeProfileId)
359+
d.Set("config_group_ids", fh.ConfigGroupIds)
342360
d.Set("content_source_id", fh.ContentSourceId)
343361
d.Set("content_view_id", fh.ContentViewId)
344362
d.Set("domain_id", fh.DomainId)

0 commit comments

Comments
 (0)