Skip to content

Commit

Permalink
Add support for hostpolicies
Browse files Browse the repository at this point in the history
Closes #55.
  • Loading branch information
oyvindhagberg committed Feb 15, 2023
1 parent ad3aed7 commit 5a6afce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.1.4 (Unreleased)
## 0.1.5 (Unreleased)

## 0.1.1 (Unreleased)

Expand Down
5 changes: 4 additions & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
mreg = {
version = "0.1.4"
version = "0.1.5"
source = "uio.no/usit/mreg"
}
}
Expand All @@ -28,6 +28,7 @@ resource "mreg_hosts" "my_hosts" {
contact = "your.email.address@example.com"
comment = "Created by the Terraform provider for Mreg"
network = "192.0.2.0/24"
policies = "without_monitoring, backup_no_backup"
}

locals {
Expand All @@ -42,6 +43,8 @@ resource "mreg_hosts" "loop_hosts" {
}
contact = "your.email.address@example.com"
comment = "Created by the Terraform provider for Mreg"
network = "192.0.2.0/24"
}

resource "mreg_hosts" "metahosts" {
# hosts without IP addresses
Expand Down
4 changes: 2 additions & 2 deletions examples/run_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ go get -v
go build
#TODO after the provider is added to the registry, there's no need to copy the file here
rm -rf ~/.terraform.d/plugins/uio.no/usit/mreg/
mkdir -p ~/.terraform.d/plugins/uio.no/usit/mreg/0.1.4/linux_amd64
cp terraform-provider-mreg ~/.terraform.d/plugins/uio.no/usit/mreg/0.1.4/linux_amd64/
mkdir -p ~/.terraform.d/plugins/uio.no/usit/mreg/0.1.5/linux_amd64
cp terraform-provider-mreg ~/.terraform.d/plugins/uio.no/usit/mreg/0.1.5/linux_amd64/
popd >/dev/null
rm -rf .terraform .terraform.lock.hcl terraform.tfstate crash.log
terraform init
Expand Down
32 changes: 29 additions & 3 deletions internal/provider/resource_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func resourceHosts() *schema.Resource {
Required: true,
ForceNew: true,
},
"policies": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -80,6 +85,16 @@ func resourceHostsCreate(ctx context.Context, d *schema.ResourceData, m interfac
comment := d.Get("comment").(string)
contact := d.Get("contact").(string)
network := d.Get("network").(string)
policies_string := d.Get("policies").(string)
policies := make([]string, 0)
if policies_string != "" {
for _, s := range strings.Split(policies_string, ",") {
s = strings.TrimSpace(s)
if s != "" {
policies = append(policies, s)
}
}
}

lock := fslock.New("terraform-provider-mreg-lockfile")
lock.Lock()
Expand Down Expand Up @@ -111,20 +126,31 @@ func resourceHostsCreate(ctx context.Context, d *schema.ResourceData, m interfac
}

// Allocate a new host object in Mreg
request := map[string]interface{}{
postdata := map[string]interface{}{
"name": hostname,
"contact": contact,
"comment": comment,
}
// Only add the ipaddress parameter if the host is supposed to have an IP address, or it will fail
if ipaddress != "" {
request["ipaddress"] = ipaddress
postdata["ipaddress"] = ipaddress
}
_, _, diags := apiClient.httpRequest("POST", "/api/v1/hosts/", request, http.StatusCreated)
_, _, diags := apiClient.httpRequest("POST", "/api/v1/hosts/", postdata, http.StatusCreated)
if len(diags) > 0 {
return diags
}

// Assign host policies, if any
for _, p := range policies {
postdata := map[string]interface{}{
"name": hostname,
}
_, _, diags := apiClient.httpRequest("POST", "/api/v1/hostpolicy/roles/"+p+"/hosts/", postdata, http.StatusCreated)
if len(diags) > 0 {
return diags
}
}

// Update the ResourceData
host["ipaddress"] = ipaddress
host["comment"] = comment
Expand Down

0 comments on commit 5a6afce

Please sign in to comment.