Skip to content

Commit

Permalink
Puppet fixes (#111)
Browse files Browse the repository at this point in the history
* Add config_group support to hosts and hostgroups
* Update documentation
  • Loading branch information
agriffit79 authored Jan 30, 2023
1 parent e5a4544 commit 4a410d1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/foreman_hostgroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The following attributes are exported:

- `architecture_id` - ID of the architecture associated with this hostgroup.
- `compute_profile_id` - ID of the compute profile associated with this hostgroup.
- `config_group_ids` - IDs of the applied config groups.
- `content_source_id` - ID of the content source associated with this hostgroup.
- `content_view_id` - ID of the content view associated with this hostgroup.
- `domain_id` - ID of the domain associated with this hostgroup.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/foreman_host.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following arguments are supported:
- `compute_attributes` - (Optional) Hypervisor specific VM options. Must be a JSON string, as every compute provider has different attributes schema
- `compute_profile_id` - (Optional)
- `compute_resource_id` - (Optional, Force New)
- `config_group_ids` - (Optional) IDs of the applied config groups.
- `domain_id` - (Optional, Force New) ID of the domain to assign to the host.
- `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`.
- `environment_id` - (Optional) ID of the environment to assign to the host.
Expand Down Expand Up @@ -55,6 +56,7 @@ The following attributes are exported:
- `compute_attributes` - Hypervisor specific VM options. Must be a JSON string, as every compute provider has different attributes schema
- `compute_profile_id` -
- `compute_resource_id` -
- `config_group_ids` - IDs of the applied config groups.
- `domain_id` - ID of the domain to assign to the host.
- `domain_name` - The domain name of the host.
- `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`.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/foreman_hostgroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following arguments are supported:

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

- `architecture_id` - ID of the architecture associated with this hostgroup.
- `compute_profile_id` - ID of the compute profile associated with this hostgroup.
- `config_group_ids` - IDs of the applied config groups.
- `content_source_id` - ID of the content source associated with this hostgroup.
- `content_view_id` - ID of the content view associated with this hostgroup.
- `domain_id` - ID of the domain associated with this hostgroup.
Expand Down
7 changes: 7 additions & 0 deletions foreman/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ type ForemanKVParameter struct {
Value string `json:"value"`
}

// JSON obect for creating and updating puppetattributes on hosts and hostgroups
type PuppetAttribute struct {
Puppetclass_ids []int `json:"puppetclass_ids"`
ConfigGroup_ids []int `json:"config_group_ids"`
}

func FromKV(kv []ForemanKVParameter) (ret map[string]string) {
ret = make(map[string]string)
for _, pair := range kv {
Expand Down Expand Up @@ -421,5 +427,6 @@ func (client *Client) WrapJSONWithTaxonomy(name interface{}, item interface{}) (
wrapped["organization_id"] = client.clientConfig.OrganizationID
log.Debugf("client.go#WrapJSONWithTaxonomy: item %+v", wrapped)
}

return json.Marshal(wrapped)
}
8 changes: 8 additions & 0 deletions foreman/api/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type ForemanHost struct {
PuppetClassIds []int `json:"puppet_class_ids,omitempty"`
// Build token
Token string `json:"token,omitempty"`
// List of config groups to apply to the hostg
ConfigGroupIds []int `json:"config_group_ids"`
// The puppetattributes object is only used for create and update, it's not populated on read, hence the duplication
PuppetAttributes PuppetAttribute `json:"puppet_attributes"`
}

// ForemanInterfacesAttribute representing a hosts defined network interfaces
Expand Down Expand Up @@ -138,6 +142,7 @@ type foremanHostDecode struct {
ForemanHost
InterfacesAttributesDecode []ForemanInterfacesAttribute `json:"interfaces"`
PuppetClassesDecode []ForemanObject `json:"puppetclasses"`
ConfigGroupsDecode []ForemanObject `json:"config_groups"`
HostParametersDecode []ForemanKVParameter `json:"parameters"`
}

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

createdHost.InterfacesAttributes = createdHost.InterfacesAttributesDecode
createdHost.PuppetClassIds = foremanObjectArrayToIdIntArray(createdHost.PuppetClassesDecode)
createdHost.ConfigGroupIds = foremanObjectArrayToIdIntArray(createdHost.ConfigGroupsDecode)
createdHost.HostParameters = createdHost.HostParametersDecode

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

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

Expand Down
7 changes: 7 additions & 0 deletions foreman/api/hostgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type ForemanHostgroup struct {
ArchitectureId int `json:"architecture_id,omitempty"`
// ID of the compute profile associated with this hostgroup
ComputeProfileId int `json:"compute_profile_id,omitempty"`
// List of config groups to apply to the hostgroup
ConfigGroupIds []int `json:"config_group_ids"`
// ID of the domain associated with this hostgroup
DomainId int `json:"domain_id,omitempty"`
// ID of the environment associated with this hostgroup
Expand Down Expand Up @@ -75,6 +77,8 @@ type ForemanHostgroup struct {
ContentSourceId int `json:"content_source_id,omitempty"`
// Map of HostGroupParameters
HostGroupParameters []ForemanKVParameter `json:"group_parameters_attributes,omitempty"`
// The puppetattributes object is only used for create and update, it's not populated on read, hence the duplication
PuppetAttributes PuppetAttribute `json:"puppet_attributes"`
}

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

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

readHostgroup.PuppetClassIds = foremanObjectArrayToIdIntArray(readHostgroup.PuppetClassesDecode)
readHostgroup.ConfigGroupIds = foremanObjectArrayToIdIntArray(readHostgroup.ConfigGroupsDecode)
readHostgroup.HostGroupParameters = readHostgroup.HostGroupParametersDecode

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

updatedHostgroup.PuppetClassIds = foremanObjectArrayToIdIntArray(updatedHostgroup.PuppetClassesDecode)
updatedHostgroup.ConfigGroupIds = foremanObjectArrayToIdIntArray(updatedHostgroup.ConfigGroupsDecode)
updatedHostgroup.HostGroupParameters = updatedHostgroup.HostGroupParametersDecode

log.Debugf("updatedHostgroup: [%+v]", updatedHostgroup)
Expand Down
20 changes: 20 additions & 0 deletions foreman/resource_foreman_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ func resourceForemanHost() *schema.Resource {
},
Description: "IDs of the applied puppet classes.",
},
"config_group_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "IDs of the applied config groups.",
},
"compute_resource_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -775,7 +784,15 @@ func buildForemanHost(d *schema.ResourceData) *api.ForemanHost {
if attr, ok = d.GetOk("puppet_class_ids"); ok {
attrSet := attr.(*schema.Set)
host.PuppetClassIds = conv.InterfaceSliceToIntSlice(attrSet.List())
host.PuppetAttributes.Puppetclass_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
}

if attr, ok = d.GetOk("config_group_ids"); ok {
attrSet := attr.(*schema.Set)
host.ConfigGroupIds = conv.InterfaceSliceToIntSlice(attrSet.List())
host.PuppetAttributes.ConfigGroup_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
}

if attr, ok = d.GetOk("parameters"); ok {
host.HostParameters = api.ToKV(attr.(map[string]interface{}))
}
Expand Down Expand Up @@ -951,6 +968,7 @@ func setResourceDataFromForemanHost(d *schema.ResourceData, fh *api.ForemanHost)
d.Set("image_id", fh.ImageId)
d.Set("model_id", fh.ModelId)
d.Set("puppet_class_ids", fh.PuppetClassIds)
d.Set("config_group_ids", fh.ConfigGroupIds)
d.Set("token", fh.Token)

setResourceDataFromForemanInterfacesAttributes(d, fh)
Expand Down Expand Up @@ -1175,6 +1193,8 @@ func resourceForemanHostUpdate(ctx context.Context, d *schema.ResourceData, meta
d.HasChange("operatingsystem_id") ||
d.HasChange("interfaces_attributes") ||
d.HasChange("build") ||
d.HasChange("puppet_class_ids") ||
d.HasChange("config_group_ids") ||
d.Get("managed") == false {

log.Debugf("host: [%+v]", h)
Expand Down
18 changes: 18 additions & 0 deletions foreman/resource_foreman_hostgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ func resourceForemanHostgroup() *schema.Resource {
Description: "ID of the compute profile associated with this hostgroup.",
},

"config_group_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "IDs of the applied config groups.",
},

"content_source_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -264,6 +274,12 @@ func buildForemanHostgroup(d *schema.ResourceData) *api.ForemanHostgroup {
hostgroup.ComputeProfileId = attr.(int)
}

if attr, ok = d.GetOk("config_group_ids"); ok {
attrSet := attr.(*schema.Set)
hostgroup.ConfigGroupIds = conv.InterfaceSliceToIntSlice(attrSet.List())
hostgroup.PuppetAttributes.ConfigGroup_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
}

if attr, ok = d.GetOk("content_source_id"); ok {
hostgroup.ContentSourceId = attr.(int)
}
Expand Down Expand Up @@ -307,6 +323,7 @@ func buildForemanHostgroup(d *schema.ResourceData) *api.ForemanHostgroup {
if attr, ok = d.GetOk("puppet_class_ids"); ok {
attrSet := attr.(*schema.Set)
hostgroup.PuppetClassIds = conv.InterfaceSliceToIntSlice(attrSet.List())
hostgroup.PuppetAttributes.Puppetclass_ids = conv.InterfaceSliceToIntSlice(attrSet.List())
}

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

0 comments on commit 4a410d1

Please sign in to comment.