Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F-359: manage template vectors #362

Merged
merged 1 commit into from
Nov 18, 2022
Merged
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
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
24 changes: 21 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,11 @@ func resourceOpennebulaClusterCreate(ctx context.Context, d *schema.ResourceData

tpl := dyn.NewTemplate()

vectorsInterface := d.Get("template_section").(*schema.Set).List()
if len(vectorsInterface) > 0 {
addTemplateVectors(vectorsInterface, tpl)
}

tagsInterface := d.Get("tags").(map[string]interface{})
for k, v := range tagsInterface {
tpl.AddPair(strings.ToUpper(k), v)
Expand Down Expand Up @@ -279,6 +285,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 +503,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
22 changes: 19 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,11 @@ func resourceOpennebulaGroupCreate(ctx context.Context, d *schema.ResourceData,
tpl.Elements = append(tpl.Elements, opennebulaVec)
}

vectorsInterface := d.Get("template_section").(*schema.Set).List()
if len(vectorsInterface) > 0 {
addTemplateVectors(vectorsInterface, tpl)
}

tagsInterface := d.Get("tags").(map[string]interface{})
for k, v := range tagsInterface {
tpl.AddPair(strings.ToUpper(k), v)
Expand Down Expand Up @@ -504,6 +510,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 +625,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",
"elements.%": "2",
"elements.testkey1": "testvalue1",
"elements.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",
"elements.%": "2",
"elements.testkey2": "testvalue2",
"elements.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",
"elements.%": "2",
"elements.testkey2": "testvalue2",
"elements.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"
elements = {
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"
elements = {
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"
elements = {
testkey2 = "testvalue2"
testkey3 = "testvalue3"
}
}
}
`

Expand Down
29 changes: 26 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,11 @@ func generateImageTemplate(d *schema.ResourceData, meta interface{}) (string, er
tpl.Add(imk.Target, val.(string))
}

vectorsInterface := d.Get("template_section").(*schema.Set).List()
if len(vectorsInterface) > 0 {
addTemplateVectors(vectorsInterface, &tpl.Template)
}

tagsInterface := d.Get("tags").(map[string]interface{})
for k, v := range tagsInterface {
tpl.AddPair(strings.ToUpper(k), v)
Expand Down
24 changes: 21 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,11 @@ func generateSecurityGroupTemplate(d *schema.ResourceData, meta interface{}) str
tpl.Add(sgk.Description, description)
}

vectorsInterface := d.Get("template_section").(*schema.Set).List()
if len(vectorsInterface) > 0 {
addTemplateVectors(vectorsInterface, &tpl.Template)
}

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