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 Nov 14, 2022
1 parent 74b0f10 commit b475924
Show file tree
Hide file tree
Showing 28 changed files with 658 additions and 28 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
FEATURES:

* **New Resource**: `opennebula_cluster` (#227)
* resources/opennebula_cluster: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_group: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_image: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_security_group: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_template: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_vm_group: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_user: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_virtual_machine: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_virtual_network: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_virtual_router: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_virtual_router_instance: add `template_section` to manage vectors with an unique key (#359)
* resources/opennebula_virtual_router_instance_template: add `template_section` to manage vectors with an unique key (#359)

DEPRECATION:

Expand Down
31 changes: 28 additions & 3 deletions opennebula/resource_opennebula_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func resourceOpennebulaCluster() *schema.Resource {
Type: schema.TypeInt,
},
},
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"template_section": templateSectionSchema(),
},
}
}
Expand Down Expand Up @@ -151,6 +152,18 @@ func resourceOpennebulaClusterCreate(ctx context.Context, d *schema.ResourceData

tpl := dyn.NewTemplate()

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 @@ -279,6 +292,11 @@ func resourceOpennebulaClusterRead(ctx context.Context, d *schema.ResourceData,
func flattenClusterTemplate(d *schema.ResourceData, meta interface{}, clusterTpl *cluster.Template) error {
config := meta.(*Configuration)

err := flattenTemplateSection(d, meta, &clusterTpl.Template)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -492,6 +510,13 @@ func resourceOpennebulaClusterUpdate(ctx context.Context, d *schema.ResourceData
}
}

if d.HasChange("template_section") {

updateTemplateSection(d, &newTpl.Template)

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down
29 changes: 26 additions & 3 deletions opennebula/resource_opennebula_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ func resourceOpennebulaGroup() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"template_section": templateSectionSchema(),
},
}
}
Expand Down Expand Up @@ -258,6 +259,18 @@ func resourceOpennebulaGroupCreate(ctx context.Context, d *schema.ResourceData,
tpl.Elements = append(tpl.Elements, opennebulaVec)
}

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 @@ -504,6 +517,11 @@ func flattenGroupTemplate(d *schema.ResourceData, meta interface{}, groupTpl *dy

}

err := flattenTemplateSection(d, meta, groupTpl)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -614,6 +632,11 @@ func resourceOpennebulaGroupUpdate(ctx context.Context, d *schema.ResourceData,
opennebulaVec := makeOpenNebulaVec(opennebula[0].(map[string]interface{}))
newTpl.Elements = append(newTpl.Elements, opennebulaVec)
}
}

if d.HasChange("template_section") {

updateTemplateSection(d, &newTpl)

update = true
}
Expand Down
43 changes: 43 additions & 0 deletions opennebula/resource_opennebula_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ 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", "template_section.*", map[string]string{
"name": "test_vec_key",
"tags.%": "2",
"tags.testkey1": "testvalue1",
"tags.testkey2": "testvalue2",
}),
),
},
{
Expand Down Expand Up @@ -76,6 +82,12 @@ 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", "template_section.*", map[string]string{
"name": "test_vec_key",
"tags.%": "2",
"tags.testkey2": "testvalue2",
"tags.testkey3": "testvalue3",
}),
),
},
{
Expand All @@ -92,6 +104,12 @@ 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", "template_section.*", map[string]string{
"name": "test_vec_key",
"tags.%": "2",
"tags.testkey2": "testvalue2",
"tags.testkey3": "testvalue3",
}),
),
},
{
Expand Down Expand Up @@ -132,6 +150,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 @@ -204,6 +223,14 @@ resource "opennebula_group" "group" {
testkey1 = "testvalue1"
testkey2 = "testvalue2"
}
template_section = {
name = "test_vec_key"
tags = {
testkey1 = "testvalue1"
testkey2 = "testvalue2"
}
}
}
`

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

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

Expand Down
36 changes: 33 additions & 3 deletions opennebula/resource_opennebula_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ func resourceOpennebulaImage() *schema.Resource {
Optional: true,
Description: "Name of the Group that onws the Image, If empty, it uses caller group",
},
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"template_section": templateSectionSchema(),
},
}
}
Expand Down Expand Up @@ -520,6 +521,16 @@ func resourceOpennebulaImageRead(ctx context.Context, d *schema.ResourceData, me
d.Set("type", image.Type)
}

err = flattenTemplateSection(d, meta, &image.Template.Template)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to flatten template section",
Detail: fmt.Sprintf("image (ID: %s): %s", d.Id(), err),
})
return diags
}

tags := make(map[string]interface{})
tagsAll := make(map[string]interface{})
for i, _ := range image.Template.Elements {
Expand Down Expand Up @@ -751,6 +762,13 @@ func resourceOpennebulaImageUpdate(ctx context.Context, d *schema.ResourceData,
update = true
}

if d.HasChange("template_section") {

updateTemplateSection(d, &tpl.Template)

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down Expand Up @@ -944,6 +962,18 @@ func generateImageTemplate(d *schema.ResourceData, meta interface{}) (string, er
tpl.Add(imk.Target, val.(string))
}

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
31 changes: 28 additions & 3 deletions opennebula/resource_opennebula_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ func resourceOpennebulaSecurityGroup() *schema.Resource {
ConflictsWith: []string{"gid"},
Description: "Name of the Group that onws the Security Group, If empty, it uses caller group",
},
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"tags": tagsSchema(),
"default_tags": defaultTagsSchemaComputed(),
"tags_all": tagsSchemaComputed(),
"template_section": templateSectionSchema(),
},
}
}
Expand Down Expand Up @@ -292,6 +293,11 @@ func flattenSecurityGroupTags(d *schema.ResourceData, meta interface{}, sgTpl *s

config := meta.(*Configuration)

err := flattenTemplateSection(d, meta, &sgTpl.Template)
if err != nil {
return err
}

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

Expand Down Expand Up @@ -467,6 +473,13 @@ func resourceOpennebulaSecurityGroupUpdate(ctx context.Context, d *schema.Resour
rulesUpdate = true
}

if d.HasChange("template_section") {

updateTemplateSection(d, &tpl.Template)

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down Expand Up @@ -684,6 +697,18 @@ func generateSecurityGroupTemplate(d *schema.ResourceData, meta interface{}) str
tpl.Add(sgk.Description, description)
}

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
7 changes: 7 additions & 0 deletions opennebula/resource_opennebula_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,13 @@ func resourceOpennebulaTemplateUpdateCustom(ctx context.Context, d *schema.Resou
update = true
}

if d.HasChange("template_section") {

updateTemplateSection(d, &newTpl.Template)

update = true
}

if d.HasChange("tags") {

oldTagsIf, newTagsIf := d.GetChange("tags")
Expand Down
Loading

0 comments on commit b475924

Please sign in to comment.