Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Support Foreman hosts that are unmanaged #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions foreman/api/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type ForemanHost struct {
BMCSuccess bool
// Additional information about this host
Comment string `json:"comment"`
// Boolean for whether Foreman manages this host
Managed bool `json:"managed"`
// Nested struct defining any interfaces associated with the Host
InterfacesAttributes []ForemanInterfacesAttribute `json:"interfaces_attributes"`
}
Expand Down Expand Up @@ -125,6 +127,7 @@ func (fh ForemanHost) MarshalJSON() ([]byte, error) {

fhMap["name"] = fh.Name
fhMap["comment"] = fh.Comment
fhMap["managed"] = fh.Managed
fhMap["build"] = fh.Build
fhMap["domain_id"] = intIdToJSONString(fh.DomainId)
fhMap["operatingsystem_id"] = intIdToJSONString(fh.OperatingSystemId)
Expand All @@ -133,9 +136,12 @@ func (fh ForemanHost) MarshalJSON() ([]byte, error) {
if len(fh.InterfacesAttributes) > 0 {
fhMap["interfaces_attributes"] = fh.InterfacesAttributes
}
log.Debugf("fhMap: [%+v]", fhMap)
hostMap := map[string]interface{}{
"host": fhMap,
}
log.Debugf("hostMap: [%+v]", fhMap)

return json.Marshal(fhMap)
return json.Marshal(hostMap)
}

// Custom JSON unmarshal function. Unmarshal to the unexported JSON struct
Expand Down Expand Up @@ -176,6 +182,9 @@ func (fh *ForemanHost) UnmarshalJSON(b []byte) error {
if fh.Comment, ok = fhMap["comment"].(string); !ok {
fh.Comment = ""
}
if fh.Managed, ok = fhMap["managed"].(bool); !ok {
fh.Managed = true
}
if _, ok = fhMap["domain_id"].(float64); !ok {
fh.DomainId = 0
} else {
Expand Down
28 changes: 23 additions & 5 deletions foreman/resource_foreman_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func resourceForemanHost() *schema.Resource {
),
},

"managed": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not this host is marked as managed by Foreman.",
},

"enable_bmc": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -274,6 +281,7 @@ func buildForemanHost(d *schema.ResourceData) *api.ForemanHost {

host.Name = d.Get("name").(string)
host.Comment = d.Get("comment").(string)
host.Managed = d.Get("managed").(bool)

if attr, ok = d.GetOk("domain_id"); ok {
host.DomainId = attr.(int)
Expand Down Expand Up @@ -427,6 +435,7 @@ func setResourceDataFromForemanHost(d *schema.ResourceData, fh *api.ForemanHost)

d.Set("name", fh.Name)
d.Set("comment", fh.Comment)
d.Set("managed", fh.Managed)
d.Set("domain_id", fh.DomainId)
d.Set("environment_id", fh.EnvironmentId)
d.Set("hostgroup_id", fh.HostgroupId)
Expand All @@ -435,6 +444,7 @@ func setResourceDataFromForemanHost(d *schema.ResourceData, fh *api.ForemanHost)
// In partial mode, flag keys below as completed successfully
d.SetPartial("name")
d.SetPartial("comment")
d.SetPartial("managed")
d.SetPartial("domain_id")
d.SetPartial("environment_id")
d.SetPartial("hostgroup_id")
Expand Down Expand Up @@ -497,8 +507,12 @@ func resourceForemanHostCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
h := buildForemanHost(d)

// NOTE(ALL): Set the build flag to true on host create
h.Build = true
// NOTE(ALL): Set the build flag to true on host create, but only for managed hosts
if h.Managed {
h.Build = true
} else {
h.Build = false
}

log.Debugf("ForemanHost: [%+v]", h)
hostRetryCount := d.Get("retry_count").(int)
Expand Down Expand Up @@ -588,8 +602,12 @@ func resourceForemanHostUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
h := buildForemanHost(d)

// NOTE(ALL): Set the build flag to true on host create
h.Build = true
// NOTE(ALL): Set the build flag to true on host update, but only for managed hosts
if h.Managed {
h.Build = true
} else {
h.Build = false
}

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

Expand Down Expand Up @@ -717,7 +735,7 @@ func resourceForemanHostDelete(d *schema.ResourceData, meta interface{}) error {
log.Debugf("deleting host that has interfaces set")
// iterate through each of the host interfaces and tag them for
// removal from the list
for idx, _ := range h.InterfacesAttributes {
for idx := range h.InterfacesAttributes {
h.InterfacesAttributes[idx].Destroy = true
}
log.Debugf("host: [%+v]", h)
Expand Down
10 changes: 8 additions & 2 deletions foreman/resource_foreman_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ func RandForemanHost() api.ForemanHost {
obj.ForemanObject = fo

obj.Build = rand.Intn(2) > 0
obj.Managed = rand.Intn(2) > 0
obj.OperatingSystemId = rand.Intn(100)
obj.DomainId = rand.Intn(100)
obj.HostgroupId = rand.Intn(100)
obj.EnvironmentId = rand.Intn(100)

obj.InterfacesAttributes = make([]api.ForemanInterfacesAttribute, rand.Intn(5))
for idx, _ := range obj.InterfacesAttributes {
for idx := range obj.InterfacesAttributes {
obj.InterfacesAttributes[idx] = api.ForemanInterfacesAttribute{
Id: rand.Intn(100),
SubnetId: rand.Intn(100),
Expand Down Expand Up @@ -316,7 +317,12 @@ func ResourceForemanHostRequestDataTestCases(t *testing.T) []TestCaseRequestData
obj = *buildForemanHost(rd)
// NOTE(ALL): See note in Create and Update functions for build flag
// override
obj.Build = true
if obj.Managed {
obj.Build = true
} else {
obj.Build = false
}

reqData, _ := json.Marshal(obj)

return []TestCaseRequestData{
Expand Down