Skip to content

Commit

Permalink
F #359: manage template sections
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Oct 26, 2022
1 parent ae56cf2 commit 17aff2f
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURES:

* **New Resource**: `opennebula_cluster` (#227)
* resources/opennebula_group: add `template_section` to manage vectors with an unique key (#359)

DEPRECATION:

Expand Down
109 changes: 109 additions & 0 deletions opennebula/resource_opennebula_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ func resourceOpennebulaGroup() *schema.Resource {
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"template_section": {
Type: schema.TypeList,
Optional: true,
Description: "Add custom section to the resource",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
Expand Down Expand Up @@ -198,6 +218,18 @@ func resourceOpennebulaGroupCreate(ctx context.Context, d *schema.ResourceData,
tpl.Elements = append(tpl.Elements, sunstoneVec)
}

vectorsInterface := d.Get("template_section").([]interface{})
for _, vectorIf := range vectorsInterface {
vector := vectorIf.(map[string]interface{})
vecName := strings.ToUpper(vector["name"].(string))
vecTags := vector["tags"].(map[string]interface{})

vec := tpl.AddVector(strings.ToUpper(vecName))
for k, v := range vecTags {
vec.AddPair(k, v)
}
}

tagsInterface := d.Get("tags").(map[string]interface{})
for k, v := range tagsInterface {
tpl.AddPair(strings.ToUpper(k), v)
Expand Down Expand Up @@ -389,6 +421,41 @@ func flattenGroupTemplate(d *schema.ResourceData, meta interface{}, groupTpl *dy

}

if vectorsInterface, ok := d.GetOk("template_section"); ok {
for _, vectorIf := range vectorsInterface.([]interface{}) {
vector := vectorIf.(map[string]interface{})
vecName := vector["name"].(string)
vecTags := vector["tags"].(map[string]interface{})

// Suppose vector key unicity
vectorTpl, err := groupTpl.GetVector(strings.ToUpper(vecName))
if err != nil {
continue
}

tags := make(map[string]interface{})
for _, pair := range vectorTpl.Pairs {
for k, _ := range vecTags {
if strings.ToUpper(k) != pair.Key() {
continue
}
tags[k] = pair.Value
break
}
}

err = d.Set("template_section", []interface{}{
map[string]interface{}{
"name": vecName,
"tags": tags,
},
})
if err != nil {
return err
}
}
}

tags := make(map[string]interface{})
tagsAll := make(map[string]interface{})

Expand Down Expand Up @@ -491,6 +558,48 @@ func resourceOpennebulaGroupUpdate(ctx context.Context, d *schema.ResourceData,
update = true
}

if d.HasChange("template_section") {
oldVectorsIf, newVectorsIf := d.GetChange("template_section")
oldVectors := oldVectorsIf.([]interface{})
newVectors := newVectorsIf.([]interface{})

// Here we suppose vector key unicity
// delete vectors
for _, oldVectorIf := range oldVectors {
oldVector := oldVectorIf.(map[string]interface{})
oldVectorName := oldVector["name"].(string)

if len(newVectors) == 0 {
newTpl.Del(strings.ToUpper(oldVectorName))
}

// if a new vector has the same name, keep it
for _, newVectorIf := range newVectors {
newVector := newVectorIf.(map[string]interface{})

if oldVectorName == newVector["name"].(string) {
continue
}
newTpl.Del(strings.ToUpper(oldVectorName))
}

}

// add/update vectors
for _, newVectorIf := range newVectors {
newVector := newVectorIf.(map[string]interface{})
newVectorName := strings.ToUpper(newVector["name"].(string))

newTpl.Del(strings.ToUpper(newVectorName))
newVec := newTpl.AddVector(newVectorName)
for k, v := range newVector["tags"].(map[string]interface{}) {
newVec.AddPair(k, v)
}
}

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down
37 changes: 37 additions & 0 deletions opennebula/resource_opennebula_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func TestAccGroup(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_group.group", "tags.%", "2"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey1", "testvalue1"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey2", "testvalue2"),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_group.group", "templace_section.*", map[string]string{
"testkey1": "testvalue1",
"testkey2": "testvalue2",
}),
),
},
{
Expand Down Expand Up @@ -67,6 +71,10 @@ func TestAccGroup(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_group.group", "tags.%", "2"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey2", "testvalue2"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey3", "testvalue3"),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_group.group", "templace_section.*", map[string]string{
"testkey2": "testvalue2",
"testkey3": "testvalue3",
}),
),
},
{
Expand All @@ -83,6 +91,10 @@ func TestAccGroup(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_group.group", "tags.%", "2"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey2", "testvalue2"),
resource.TestCheckResourceAttr("opennebula_group.group", "tags.testkey3", "testvalue3"),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_group.group", "templace_section.*", map[string]string{
"testkey2": "testvalue2",
"testkey3": "testvalue3",
}),
),
},
{
Expand Down Expand Up @@ -123,6 +135,7 @@ func TestAccGroup(t *testing.T) {
"views": "cloud",
}),
resource.TestCheckResourceAttr("opennebula_group.group2", "tags.%", "0"),
resource.TestCheckResourceAttr("opennebula_group.group2", "template_section.#", "0"),
),
},
},
Expand Down Expand Up @@ -190,6 +203,14 @@ resource "opennebula_group" "group" {
testkey1 = "testvalue1"
testkey2 = "testvalue2"
}
template_section = {
name = "test_vec_key"
tags = {
testkey1 = "testvalue1"
testkey2 = "testvalue2"
}
}
}
`

Expand Down Expand Up @@ -217,6 +238,14 @@ resource "opennebula_group" "group" {
testkey2 = "testvalue2"
testkey3 = "testvalue3"
}
template_section = {
name = "test_vec_key"
tags = {
testkey2 = "testvalue2"
testkey3 = "testvalue3"
}
}
}
`

Expand Down Expand Up @@ -244,6 +273,14 @@ resource "opennebula_group" "group" {
testkey2 = "testvalue2"
testkey3 = "testvalue3"
}
template_section = {
name = "test_vec_key"
tags = {
testkey2 = "testvalue2"
testkey3 = "testvalue3"
}
}
}
`

Expand Down
18 changes: 17 additions & 1 deletion website/docs/r/group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ resource "opennebula_group" "example" {
tags = {
environment = "example"
}
template_section {
name = "test"
tags = {
tag1 = "value1"
}
}
}
```

Expand All @@ -64,6 +72,7 @@ The following arguments are supported:
* `quotas` - (Optional) See [Quotas parameters](#quotas-parameters) below for details
* `sunstone` - (Optional) Allow users and group admins to access specific views. See [Sunstone parameters](#sunstone-parameters) below for details
* `tags` - (Optional) Group tags (Key = value)
* `template_section` - (Optional) Allow to add a custom vector. See [Template section parameters](#template-section-parameters)

### Quotas parameters

Expand Down Expand Up @@ -108,13 +117,20 @@ The following arguments are supported:
* `running_vms` - (Optional) Number of Virtual Machines allowed in `RUNNING` state. Defaults to `default quota`.
* `system_disk_size` - (Optional) Maximum disk global size (in MB) allowed on a `SYSTEM` datastore. Defaults to `default quota`.

#### Sunstone parameters
### Sunstone parameters

* `default_view` - (Optional) Default Sunstone view for regular users
* `views` - (Optional) List of available views for regular users
* `group_admin_default_view` - (Optional) Default Sunstone view for group admin users
* `group_admin_views` - (Optional) List of available views for the group admins

### Template section parameters

`template_section` supports the following arguments:

* `name` - (Optional) The vector name.
* `tags` - (Optional) Collection of custom tags.

## Attribute Reference

The following attribute is exported:
Expand Down

0 comments on commit 17aff2f

Please sign in to comment.