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

Org VDC import, data source and read fix #324

Merged
merged 48 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6585559
Add missing file
vbauzys Jul 15, 2019
e247d25
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
vbauzys Jul 15, 2019
cf1e7c2
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Jul 23, 2019
be6790f
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
vbauzys Jul 24, 2019
6f4c5cb
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
vbauzys Jul 25, 2019
c73d402
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
vbauzys Jul 26, 2019
261d142
git push origin master
vbauzys Jul 29, 2019
c431d43
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Aug 1, 2019
414b3bf
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Aug 15, 2019
8b03ab8
Merge branch 'master' of github.com:terraform-providers/terraform-pro…
vbauzys Aug 22, 2019
36c5487
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Sep 3, 2019
db397a2
First implementation - import, data source, read fix
vbauzys Sep 6, 2019
9180dc2
Moved function to govcd
vbauzys Sep 9, 2019
db01eec
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Sep 9, 2019
638d286
Merge branch 'master' into import_vdc
vbauzys Sep 9, 2019
4db6be4
Fix according latest govcd
vbauzys Sep 9, 2019
558c726
Change to use new StringMap introduced
vbauzys Sep 9, 2019
7875bfd
Fix error messages
vbauzys Sep 9, 2019
0eac92d
Fix doc
vbauzys Sep 9, 2019
c232e4f
Bump govcd version
vbauzys Sep 10, 2019
c2ea184
Improve doc
vbauzys Sep 10, 2019
cc66000
Fix according review
vbauzys Sep 10, 2019
8270605
Improve test
vbauzys Sep 10, 2019
69a1307
Improve test
vbauzys Sep 10, 2019
1383c19
Rearange test principles
vbauzys Sep 10, 2019
81e1e2b
Improve test
vbauzys Sep 10, 2019
8af6b38
Improve test
vbauzys Sep 10, 2019
f40293f
Improve test
vbauzys Sep 10, 2019
c5c829a
Change to use internal hash functionality
vbauzys Sep 11, 2019
5b3da56
Changes with Giuseppe suggestions
vbauzys Sep 12, 2019
4325821
Move test
vbauzys Sep 12, 2019
e12e4d3
vendor changes by using go 1.13
vbauzys Sep 12, 2019
331e895
git push origin masterMerge branch 'master' of github.com:terraform-p…
vbauzys Sep 12, 2019
87be04c
Merge branch 'master' into import_vdc
vbauzys Sep 12, 2019
9f643aa
Add comment
vbauzys Sep 12, 2019
4d67b97
Add comment
vbauzys Sep 12, 2019
71e54a1
Improved test
vbauzys Sep 12, 2019
af69dbf
Improve comment
vbauzys Sep 12, 2019
1de8c02
Convert from typeSet to typeList
vbauzys Sep 17, 2019
e0dad07
Improvements
vbauzys Sep 18, 2019
7a6bd51
Removed not used anymore
vbauzys Sep 18, 2019
847e4d8
Improve tests
vbauzys Sep 18, 2019
c46bb79
Improve tests
vbauzys Sep 18, 2019
d2468fb
Improve tests
vbauzys Sep 18, 2019
72adf38
Add import documentation
vbauzys Sep 18, 2019
7b87d2b
Fixes
vbauzys Sep 18, 2019
83a9b0f
Added output
vbauzys Sep 18, 2019
f6bfd64
Added supported version
vbauzys Sep 18, 2019
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FEATURES:
* **New Data Source:** `vcd_org` Organization - ([#218])
* **New Data Source:** `vcd_catalog` Catalog - ([#218])
* **New Data Source:** `vcd_catalog_item` CatalogItem - ([#218])
* **New Data Source:** `vcd_org_vdc` Organization VDC - ([#324])
* **New Data Source:** `vcd_external_network` External Network - ([#218])

IMPROVEMENTS:
Expand All @@ -22,6 +23,8 @@ IMPROVEMENTS:
* `resource/vcd_vapp_vm` allows to force guest customization [#310]
* `resource/vcd_vapp` supports guest properties [#319]
* `resource/vcd_vapp_vm` supports guest properties [#319]
* Upgrade Terraform SDK dependency to 0.12.6 [#302]
* `vcd_org_vdc` Add import capability - [#324]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting it gets the "read" capability as someone may be surprised getting changes after regular terraform apply

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note

* Upgrade Terraform SDK dependency to 0.12.8 [#320]

BUG FIXES:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.13

require (
github.com/hashicorp/terraform v0.12.8
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.6
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4A
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.6 h1:toIxeZImxsZpNI2pWeOss/D4MrYfOLx6gy0/bdfPaII=
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.6/go.mod h1:HonlGxbjJ1NAibWh99eE4/S2l6ZOZ5KJzKK1rh2a9vc=
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.7 h1:/nKChkxGrak1cF2Vei92ubPd4ciRSX6bgxywF6aBBnE=
github.com/vmware/go-vcloud-director/v2 v2.4.0-alpha.7/go.mod h1:HonlGxbjJ1NAibWh99eE4/S2l6ZOZ5KJzKK1rh2a9vc=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
Expand Down
18 changes: 18 additions & 0 deletions vcd/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,21 @@ func getMajorVersion() string {
// Where the first element is the full text matched, and the second one is the first captured text
return versionList[0][1]
}

// getHashValuesFromKey returns the hash values of a TypeSet Key
// Given "parent.1234.child.5678.something", it will return "1234" and "5678"
// for Example: compute_capacity.315866465.memory.508945747.limit
// (returns 315866465 and 508945747)
// Warning this function works fine when there is only one key with parent
func getHashValuesFromKey(stateFileMap map[string]string, parentKey, childKey string) (string, string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a warning and explanation on how function deals with situations where there are more than one instance of the TypeSet element. In compute_capacity it cannot happen because it has MaxItems: 1, but in other resources it is possible. For an example, take a look at vcd_network_routed.static_ip_pool (of which you can define many).

E.g.:

# Testing network with more than one IP pool
resource "vcd_network_routed" "demo_routed_net_multi_pool" {
  org = "${vcd_org.demo_org.name}"
  vdc = "${vcd_org_vdc.demo_vdc.name}"

  name         = "demo-net-multi_pool"
  edge_gateway = "${vcd_edgegateway.demo_gw.name}"

  gateway = "192.168.10.1"

  static_ip_pool {
    start_address = "192.168.10.2"
    end_address   = "192.168.10.10"
  }

  static_ip_pool {
    start_address = "192.168.10.20"
    end_address   = "192.168.10.30"
  }

  static_ip_pool {
    start_address = "192.168.10.50"
    end_address   = "192.168.10.100"
  }
}

Finally, more and more it seems that the TypeSet is not the right type for fields that are sets, but we want only a single instance of them. Maybe there's a better type for such case? TypeMap?
https://www.terraform.io/docs/extend/schemas/schema-types.html

CC: @dataclouder @Didainius

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_ip_pool I think should be typeList of StringMaps. vCD case as we talked typeSet is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added warning

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_ip_pool I think should be typeList of StringMaps. vCD case as we talked typeSet is ok.

When you say "should", you mean we should change it? Because right now it's:

			"static_ip_pool": &schema.Schema{
				Type:     schema.TypeSet,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess now this gets to a question if this is used at all. Although it may come in handy in some other place in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


var searchPattern = regexp.MustCompile(parentKey + `\.(\d+)\.` + childKey + `\.(\d+)\.\w+`)
for k, _ := range stateFileMap {
foundList := searchPattern.FindAllStringSubmatch(k, -1)
if len(foundList) > 0 && len(foundList[0]) > 0 {
return foundList[0][1], foundList[0][2], nil
}
}

return "", "", fmt.Errorf("error extracting hashes from '%s', err %s", parentKey, childKey)
}
10 changes: 5 additions & 5 deletions vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,18 +541,18 @@ func createSuiteCatalogAndItem(config TestConfig) {
panic(err)
}

var catalog govcd.Catalog
var catalog *govcd.Catalog

catalogPreserved := true
_, err = org.GetCatalogByName(testSuiteCatalogName, false)
catalog, err = org.GetCatalogByName(testSuiteCatalogName, false)
if err != nil {
catalogPreserved = false
}

if testConfig.VCD.Catalog.Name == "" && !catalogPreserved {
fmt.Printf("Creating catalog for test suite...\n")
catalog, err = org.CreateCatalog(testSuiteCatalogName, "Test suite purpose")
if err != nil || catalog == (govcd.Catalog{}) {
*catalog, err = org.CreateCatalog(testSuiteCatalogName, "Test suite purpose")
if err != nil || *catalog == (govcd.Catalog{}) {
panic(err)
}
fmt.Printf("Catalog created successfully\n")
Expand All @@ -566,7 +566,7 @@ func createSuiteCatalogAndItem(config TestConfig) {
panic(err)
}

catalog = *existingCatalog
catalog = existingCatalog
fmt.Printf("Catalog found successfully\n")
testSuiteCatalogName = testConfig.VCD.Catalog.Name
} else {
Expand Down
206 changes: 206 additions & 0 deletions vcd/datasource_vcd_org_vdc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package vcd

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
)

func datasourceVcdOrgVdc() *schema.Resource {
capacityWithUsage := schema.Schema{
Type: schema.TypeList,
Computed: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allocated": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity that is committed to be available. Value in MB or MHz. Used with AllocationPool (Allocation pool) and ReservationPool (Reservation pool).",
},
"limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Capacity limit relative to the value specified for Allocation. It must not be less than that value. If it is greater than that value, it implies over provisioning. A value of 0 specifies unlimited units. Value in MB or MHz. Used with AllocationVApp (Pay as you go).",
},
"reserved": {
Type: schema.TypeInt,
Computed: true,
},
"used": {
Type: schema.TypeInt,
Computed: true,
},
"overhead": {
Type: schema.TypeInt,
Computed: true,
},
},
},
}

return &schema.Resource{
Read: datasourceVcdOrgVdcRead,

Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
Description: "Organization to create the VDC in",
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"allocation_model": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The allocation model used by this VDC; must be one of {AllocationVApp, AllocationPool, ReservationPool}",
},
"compute_capacity": &schema.Schema{
Computed: true,
MinItems: 1,
MaxItems: 1,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu": &capacityWithUsage,
"memory": &capacityWithUsage,
},
},
Description: "The compute capacity allocated to this VDC.",
},
"nic_quota": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Maximum number of virtual NICs allowed in this VDC. Defaults to 0, which specifies an unlimited number.",
},
"network_quota": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Maximum number of network objects that can be deployed in this VDC. Defaults to 0, which means no networks can be deployed.",
},
"vm_quota": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of VMs that can be created in this VDC. Includes deployed and undeployed VMs in vApps and vApp templates. Defaults to 0, which specifies an unlimited number.",
},
"enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "True if this VDC is enabled for use by the organization VDCs. Default is true.",
},
"storage_profile": &schema.Schema{
Type: schema.TypeList,
Computed: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of Provider VDC storage profile.",
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this storage profile is enabled for use in the VDC.",
},
"limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Maximum number of MB allocated for this storage profile. A value of 0 specifies unlimited MB.",
},
"default": {
Type: schema.TypeBool,
Computed: true,
Description: "True if this is default storage profile for this VDC. The default storage profile is used when an object that can specify a storage profile is created with no storage profile specified.",
},
},
},
Description: "Storage profiles supported by this VDC.",
},
"memory_guaranteed": &schema.Schema{
Type: schema.TypeFloat,
Computed: true,
Description: "Percentage of allocated memory resources guaranteed to vApps deployed in this VDC. " +
"For example, if this value is 0.75, then 75% of allocated resources are guaranteed. " +
"Required when AllocationModel is AllocationVApp or AllocationPool. When Allocation model is AllocationPool minimum value is 0.2. If the element is empty, vCD sets a value.",
},
"cpu_guaranteed": &schema.Schema{
Type: schema.TypeFloat,
Computed: true,
Description: "Percentage of allocated CPU resources guaranteed to vApps deployed in this VDC. " +
"For example, if this value is 0.75, then 75% of allocated resources are guaranteed. " +
"Required when AllocationModel is AllocationVApp or AllocationPool. If the element is empty, vCD sets a value",
},
"cpu_speed": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "Specifies the clock frequency, in Megahertz, for any virtual CPU that is allocated to a VM. A VM with 2 vCPUs will consume twice as much of this value. Ignored for ReservationPool. Required when AllocationModel is AllocationVApp or AllocationPool, and may not be less than 256 MHz. Defaults to 1000 MHz if the element is empty or missing.",
},
"enable_thin_provisioning": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Boolean to request thin provisioning. Request will be honored only if the underlying datastore supports it. Thin provisioning saves storage space by committing it on demand. This allows over-allocation of storage.",
},
"network_pool_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of a network pool in the Provider VDC. Required if this VDC will contain routed or isolated networks.",
},
"provider_vdc_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A reference to the Provider VDC from which this organization VDC is provisioned.",
},
"enable_fast_provisioning": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Request for fast provisioning. Request will be honored only if the underlying datas tore supports it. Fast provisioning can reduce the time it takes to create virtual machines by using vSphere linked clones. If you disable fast provisioning, all provisioning operations will result in full clones.",
},
// Always null in the response to a GET request. On update, set to false to disallow the update if the AllocationModel is AllocationPool or ReservationPool
// and the ComputeCapacity you specified is greater than what the backing Provider VDC can supply. Defaults to true if empty or missing.
"allow_over_commit": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Set to false to disallow creation of the VDC if the AllocationModel is AllocationPool or ReservationPool and the ComputeCapacity you specified is greater than what the backing Provider VDC can supply. Default is true.",
},
"enable_vm_discovery": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "True if discovery of vCenter VMs is enabled for resource pools backing this VDC. If left unspecified, the actual behaviour depends on enablement at the organization level and at the system level.",
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Description: "Key and value pairs for Org VDC metadata",
},
},
}
}

func datasourceVcdOrgVdcRead(d *schema.ResourceData, meta interface{}) error {
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
vcdClient := meta.(*VCDClient)

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
if err != nil {
return fmt.Errorf(errorRetrievingOrg, err)
}

adminVdc, err := adminOrg.GetAdminVDCByName(d.Get("name").(string), false)
if err != nil {
log.Printf("[DEBUG] Unable to find VDC")
return fmt.Errorf("unable to find VDC %s", err)
}

d.SetId(adminVdc.AdminVdc.ID)

return setOrgVdcData(d, vcdClient, adminOrg, adminVdc)
}
Loading