├── .github ├── ISSUE_TEMPLATE │ ├── bug.yml │ ├── config.yml │ ├── docs.yml │ └── enhancement.yml ├── dependabot.yml ├── labeler-issues.yml ├── labeler-pull-requests.yml ├── pull_request_template.md └── workflows │ ├── docs.yml │ ├── golangci-lint.yml │ ├── issue-comment-created.yml │ ├── issue-opened.yml │ ├── lock.yml │ ├── milestone-closed.yml │ ├── pull-requests.yml │ ├── release.yml │ ├── remove-issue-label.yml │ ├── stale.yml │ └── test.yml ├── .gitignore ├── .golangci.yml ├── .goreleaser.tmpl ├── .goreleaser.yml ├── CHANGELOG.md ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── NOTICE ├── README.md ├── docs ├── build.md ├── data-sources │ ├── compute_cluster.md │ ├── compute_cluster_host_group.md │ ├── content_library.md │ ├── content_library_item.md │ ├── custom_attribute.md │ ├── datacenter.md │ ├── datastore.md │ ├── datastore_cluster.md │ ├── datastore_stats.md │ ├── distributed_virtual_switch.md │ ├── dynamic.md │ ├── folder.md │ ├── guest_os_customization.md │ ├── host.md │ ├── host_base_images.md │ ├── host_pci_device.md │ ├── host_thumbprint.md │ ├── host_vgpu_profile.md │ ├── license.md │ ├── network.md │ ├── ovf_vm_template.md │ ├── resource_pool.md │ ├── storage_policy.md │ ├── tag.md │ ├── tag_category.md │ ├── vapp_container.md │ ├── virtual_machine.md │ ├── vmfs_disks.md │ └── vsphere_role.md ├── images │ ├── icon-color.svg │ └── icon-white.svg ├── index.md ├── install.md └── resources │ ├── compute_cluster.md │ ├── compute_cluster_host_group.md │ ├── compute_cluster_vm_affinity_rule.md │ ├── compute_cluster_vm_anti_affinity_rule.md │ ├── compute_cluster_vm_dependency_rule.md │ ├── compute_cluster_vm_group.md │ ├── compute_cluster_vm_host_rule.md │ ├── content_library.md │ ├── content_library_item.md │ ├── custom_attribute.md │ ├── datacenter.md │ ├── datastore_cluster.md │ ├── datastore_cluster_vm_anti_affinity_rule.md │ ├── distributed_port_group.md │ ├── distributed_virtual_switch.md │ ├── dpm_host_override.md │ ├── drs_vm_override.md │ ├── file.md │ ├── folder.md │ ├── guest_os_customization.md │ ├── ha_vm_override.md │ ├── host.md │ ├── host_port_group.md │ ├── host_virtual_switch.md │ ├── license.md │ ├── nas_datastore.md │ ├── offline_software_depot.md │ ├── resource_pool.md │ ├── storage_drs_vm_override.md │ ├── supervisor.md │ ├── tag.md │ ├── tag_category.md │ ├── vapp_container.md │ ├── vapp_entity.md │ ├── virtual_disk.md │ ├── virtual_machine.md │ ├── virtual_machine_class.md │ ├── virtual_machine_snapshot.md │ ├── vm_storage_policy.md │ ├── vmfs_datastore.md │ ├── vnic.md │ ├── vsphere_entity_permissions.md │ └── vsphere_role.md ├── go.mod ├── go.sum ├── main.go ├── scripts ├── docscheck.sh └── gofmtcheck.sh ├── terraform-registry-manifest.json ├── tests ├── README.md ├── main.tf ├── run_tests.sh ├── setup_env_vars.sh └── variables.tf └── vsphere ├── config.go ├── config_test.go ├── data_source_vsphere_compute_cluster.go ├── data_source_vsphere_compute_cluster_host_group.go ├── data_source_vsphere_compute_cluster_host_group_test.go ├── data_source_vsphere_compute_cluster_test.go ├── data_source_vsphere_content_library.go ├── data_source_vsphere_content_library_item.go ├── data_source_vsphere_content_library_item_test.go ├── data_source_vsphere_content_library_test.go ├── data_source_vsphere_custom_attribute.go ├── data_source_vsphere_custom_attribute_test.go ├── data_source_vsphere_datacenter.go ├── data_source_vsphere_datacenter_test.go ├── data_source_vsphere_datastore.go ├── data_source_vsphere_datastore_cluster.go ├── data_source_vsphere_datastore_cluster_test.go ├── data_source_vsphere_datastore_stats.go ├── data_source_vsphere_datastore_stats_test.go ├── data_source_vsphere_datastore_test.go ├── data_source_vsphere_distributed_virtual_switch.go ├── data_source_vsphere_distributed_virtual_switch_test.go ├── data_source_vsphere_dynamic.go ├── data_source_vsphere_dynamic_test.go ├── data_source_vsphere_folder.go ├── data_source_vsphere_folder_test.go ├── data_source_vsphere_guest_os_customization.go ├── data_source_vsphere_guest_os_customization_test.go ├── data_source_vsphere_host.go ├── data_source_vsphere_host_base_images.go ├── data_source_vsphere_host_pci_device.go ├── data_source_vsphere_host_test.go ├── data_source_vsphere_host_thumbprint.go ├── data_source_vsphere_host_thumbprint_test.go ├── data_source_vsphere_host_vgpu_profile.go ├── data_source_vsphere_license.go ├── data_source_vsphere_license_test.go ├── data_source_vsphere_network.go ├── data_source_vsphere_network_test.go ├── data_source_vsphere_ovf_vm_template.go ├── data_source_vsphere_resource_pool.go ├── data_source_vsphere_resource_pool_test.go ├── data_source_vsphere_role.go ├── data_source_vsphere_role_test.go ├── data_source_vsphere_storage_policy.go ├── data_source_vsphere_storage_policy_test.go ├── data_source_vsphere_tag.go ├── data_source_vsphere_tag_category.go ├── data_source_vsphere_tag_category_test.go ├── data_source_vsphere_tag_test.go ├── data_source_vsphere_vapp_container.go ├── data_source_vsphere_vapp_container_test.go ├── data_source_vsphere_virtual_machine.go ├── data_source_vsphere_virtual_machine_test.go ├── data_source_vsphere_vmfs_disks.go ├── data_source_vsphere_vmfs_disks_test.go ├── datacenter_helper.go ├── datastore_structure.go ├── distributed_port_group_structure.go ├── distributed_virtual_port_setting_structure.go ├── distributed_virtual_switch_helper.go ├── distributed_virtual_switch_structure.go ├── event_helper.go ├── helper_test.go ├── host_data_store_system_helper.go ├── host_nas_volume_structure.go ├── host_network_policy_structure.go ├── host_network_system_helper.go ├── host_port_group_structure.go ├── host_storage_system_helper.go ├── host_virtual_switch_structure.go ├── internal ├── administrationroles │ └── vsphere_permission_subresource.go ├── helper │ ├── clustercomputeresource │ │ └── cluster_compute_resource_helper.go │ ├── computeresource │ │ └── compute_resource_helper.go │ ├── contentlibrary │ │ └── content_library_helper.go │ ├── customattribute │ │ └── custom_attributes_helper.go │ ├── datacenter │ │ └── datacenter_helper.go │ ├── datastore │ │ └── datastore_helper.go │ ├── dvportgroup │ │ └── distributed_port_group_helper.go │ ├── envbrowse │ │ └── environment_browser_helper.go │ ├── folder │ │ └── folder_helper.go │ ├── guestoscustomizations │ │ └── customizations_helper.go │ ├── hostsystem │ │ └── host_system_helper.go │ ├── license │ │ └── license_helper.go │ ├── network │ │ └── network_helper.go │ ├── nsx │ │ └── nsx_helper.go │ ├── ovfdeploy │ │ └── ovf_helper.go │ ├── provider │ │ └── provider_helper.go │ ├── resourcepool │ │ └── resource_pool_helper.go │ ├── spbm │ │ └── spbm_helper.go │ ├── storagepod │ │ └── storage_pod_helper.go │ ├── structure │ │ └── structure_helper.go │ ├── testhelper │ │ ├── constants.go │ │ └── testing.go │ ├── utils │ │ └── entityIdToManagedObjectId.go │ ├── vappcontainer │ │ └── vappcontainer_helper.go │ ├── viapi │ │ ├── vim_helper.go │ │ └── vim_helper_test.go │ ├── virtualdisk │ │ ├── virtual_disk_helper.go │ │ └── virtual_disk_helper_test.go │ ├── virtualmachine │ │ └── virtual_machine_helper.go │ ├── vsanclient │ │ └── vsan_client_helper.go │ └── vsansystem │ │ └── vsansystem_helper.go ├── virtualdevice │ ├── virtual_machine_cdrom_subresource.go │ ├── virtual_machine_device_subresource.go │ ├── virtual_machine_disk_subresource.go │ ├── virtual_machine_disk_subresource_test.go │ ├── virtual_machine_network_interface_subresource.go │ └── virtual_machine_tag_rules_subresource.go └── vmworkflow │ ├── virtual_machine_clone_subresource.go │ └── virtual_machine_ovfdeploy_subresource.go ├── nas_datastore_helper.go ├── provider.go ├── provider_test.go ├── resource_vsphere_compute_cluster.go ├── resource_vsphere_compute_cluster_host_group.go ├── resource_vsphere_compute_cluster_host_group_test.go ├── resource_vsphere_compute_cluster_test.go ├── resource_vsphere_compute_cluster_vm_affinity_rule.go ├── resource_vsphere_compute_cluster_vm_affinity_rule_test.go ├── resource_vsphere_compute_cluster_vm_anti_affinity_rule.go ├── resource_vsphere_compute_cluster_vm_anti_affinity_rule_test.go ├── resource_vsphere_compute_cluster_vm_dependency_rule.go ├── resource_vsphere_compute_cluster_vm_dependency_rule_test.go ├── resource_vsphere_compute_cluster_vm_group.go ├── resource_vsphere_compute_cluster_vm_group_test.go ├── resource_vsphere_compute_cluster_vm_host_rule.go ├── resource_vsphere_compute_cluster_vm_host_rule_test.go ├── resource_vsphere_content_library.go ├── resource_vsphere_content_library_item.go ├── resource_vsphere_content_library_item_test.go ├── resource_vsphere_content_library_test.go ├── resource_vsphere_custom_attribute.go ├── resource_vsphere_custom_attribute_test.go ├── resource_vsphere_datacenter.go ├── resource_vsphere_datacenter_test.go ├── resource_vsphere_datastore_cluster.go ├── resource_vsphere_datastore_cluster_test.go ├── resource_vsphere_datastore_cluster_vm_anti_affinity_rule.go ├── resource_vsphere_datastore_cluster_vm_anti_affinity_rule_test.go ├── resource_vsphere_distributed_port_group.go ├── resource_vsphere_distributed_port_group_test.go ├── resource_vsphere_distributed_virtual_switch.go ├── resource_vsphere_distributed_virtual_switch_pvlan_mapping.go ├── resource_vsphere_distributed_virtual_switch_pvlan_mapping_test.go ├── resource_vsphere_distributed_virtual_switch_test.go ├── resource_vsphere_dpm_host_override.go ├── resource_vsphere_dpm_host_override_test.go ├── resource_vsphere_drs_vm_override.go ├── resource_vsphere_drs_vm_override_test.go ├── resource_vsphere_entity_permissions.go ├── resource_vsphere_entity_permissions_test.go ├── resource_vsphere_file.go ├── resource_vsphere_file_test.go ├── resource_vsphere_folder.go ├── resource_vsphere_folder_test.go ├── resource_vsphere_guest_os_customization.go ├── resource_vsphere_guest_os_customization_test.go ├── resource_vsphere_ha_vm_override.go ├── resource_vsphere_ha_vm_override_test.go ├── resource_vsphere_host.go ├── resource_vsphere_host_port_group.go ├── resource_vsphere_host_port_group_test.go ├── resource_vsphere_host_test.go ├── resource_vsphere_host_virtual_switch.go ├── resource_vsphere_host_virtual_switch_test.go ├── resource_vsphere_license.go ├── resource_vsphere_license_test.go ├── resource_vsphere_nas_datastore.go ├── resource_vsphere_nas_datastore_test.go ├── resource_vsphere_offline_software_depot.go ├── resource_vsphere_offline_software_depot_test.go ├── resource_vsphere_resource_pool.go ├── resource_vsphere_resource_pool_test.go ├── resource_vsphere_role.go ├── resource_vsphere_role_test.go ├── resource_vsphere_storage_drs_vm_override.go ├── resource_vsphere_storage_drs_vm_override_test.go ├── resource_vsphere_supervisor.go ├── resource_vsphere_supervisor_test.go ├── resource_vsphere_tag.go ├── resource_vsphere_tag_category.go ├── resource_vsphere_tag_category_test.go ├── resource_vsphere_tag_test.go ├── resource_vsphere_vapp_container.go ├── resource_vsphere_vapp_container_test.go ├── resource_vsphere_vapp_entity.go ├── resource_vsphere_vapp_entity_test.go ├── resource_vsphere_virtual_disk.go ├── resource_vsphere_virtual_disk_test.go ├── resource_vsphere_virtual_machine.go ├── resource_vsphere_virtual_machine_class.go ├── resource_vsphere_virtual_machine_class_test.go ├── resource_vsphere_virtual_machine_snapshot.go ├── resource_vsphere_virtual_machine_snapshot_test.go ├── resource_vsphere_virtual_machine_test.go ├── resource_vsphere_vm_storage_policy.go ├── resource_vsphere_vm_storage_policy_test.go ├── resource_vsphere_vmfs_datastore.go ├── resource_vsphere_vmfs_datastore_test.go ├── resource_vsphere_vnic.go ├── resource_vsphere_vnic_test.go ├── tags_helper.go ├── testdata └── .gitkeep ├── util.go ├── virtual_machine_config_structure.go ├── virtual_machine_guest_structure.go └── vsphere_sweep_test.go /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | blank_issues_enabled: false 3 | contact_links: 4 | - name: Community Discussions 5 | url: https://github.com/vmware/terraform-provider-vsphere/discussions 6 | about: Need some help? Join the community discussions. 7 | - name: Terraform Provider Documentation 8 | url: https://registry.terraform.io/providers/vmware/vsphere/latest/docs 9 | about: Having trouble with this Terraform provider? Check out the provider documentation. 10 | - name: Terraform Documentation 11 | url: https://developer.hashicorp.com/terraform/docs 12 | about: Having trouble with Terraform? Check out the product documentation. 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/docs.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Documentation 3 | description: Found a typo or something that needs clarification? 4 | labels: 5 | - [documentation, needs-review] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: > 10 | When filing a documentation issue, please include the following information. 11 | - type: checkboxes 12 | id: terms 13 | attributes: 14 | label: Code of Conduct 15 | description: >- 16 | This project has a [Code of Conduct](https://github.com/vmware/terraform-provider-vsphere/blob/main/CODE_OF_CONDUCT.md) 17 | that all participants are expected to understand and follow. 18 | options: 19 | - label: I have read and agree to the project's Code of Conduct. 20 | required: true 21 | - type: input 22 | id: version-provider 23 | attributes: 24 | label: Provider Version 25 | description: Please provide the provider version. 26 | placeholder: e.g. x.y.z 27 | validations: 28 | required: true 29 | - type: textarea 30 | id: motivation 31 | attributes: 32 | label: Motivation 33 | description: Why should we update our docs or examples? 34 | validations: 35 | required: false 36 | - type: textarea 37 | id: suggestion 38 | attributes: 39 | label: Suggestion 40 | description: What should we do instead? 41 | validations: 42 | required: false 43 | - type: markdown 44 | attributes: 45 | value: "### Community Note\n* Please vote on this issue by adding a \U0001F44D [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request\n* Please do not leave \"+1\" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request\n* If you are interested in working on this issue or have submitted a pull request, please leave a comment\n" 46 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/enhancement.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Enhancement Request 3 | description: Is something critical missing? Suggest an enhancement. 4 | labels: 5 | - [enhancement, needs-review] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: > 10 | Before filing an enhancement, please search the existing issues and use the 11 | [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) 12 | feature to add up-votes to existing requests. 13 | 14 | 15 | When filing an enhancement, please include the following information. 16 | - type: checkboxes 17 | id: terms 18 | attributes: 19 | label: Code of Conduct 20 | description: >- 21 | This project has a [Code of Conduct](https://github.com/vmware/terraform-provider-vsphere/blob/main/CODE_OF_CONDUCT.md) 22 | that all participants are expected to understand and follow. 23 | options: 24 | - label: I have read and agree to the project's Code of Conduct. 25 | required: true 26 | - label: Vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue initial description to help the maintainers prioritize. 27 | required: false 28 | - label: Do not leave "+1" or other comments that do not add relevant information or questions. 29 | required: false 30 | - label: If you are interested in working on this issue or have submitted a pull request, please leave a comment. 31 | required: false 32 | - type: textarea 33 | id: description 34 | attributes: 35 | label: Description 36 | description: A written overview of the enhancement. 37 | validations: 38 | required: true 39 | - type: textarea 40 | id: use-case 41 | attributes: 42 | label: Use Case(s) 43 | description: Any relevant use-cases that you see. 44 | validations: 45 | required: true 46 | - type: textarea 47 | id: potential-configuration 48 | attributes: 49 | label: Potential Configuration 50 | description: Provide a potential configuration. 51 | validations: 52 | required: true 53 | - type: textarea 54 | id: references 55 | attributes: 56 | label: References 57 | description: Provide any references. 58 | validations: 59 | required: false 60 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: weekly 7 | labels: 8 | - chore 9 | - github-actions 10 | commit-message: 11 | prefix: "chore(gh)" 12 | include: "scope" 13 | - package-ecosystem: gomod 14 | directory: / 15 | groups: 16 | terraform: 17 | patterns: 18 | - github.com/hashicorp/terraform-plugin-* 19 | golang-x: 20 | patterns: 21 | - golang.org/x/* 22 | google-golang: 23 | patterns: 24 | - google.golang.org/* 25 | ignore: 26 | # go-cty should only be updated via terraform-plugin-sdk 27 | - dependency-name: github.com/hashicorp/go-cty 28 | # hcl/v2 should only be updated via terraform-plugin-sdk 29 | - dependency-name: github.com/hashicorp/hcl/v2 30 | # terraform-plugin-go should only be updated via terraform-plugin-framework 31 | - dependency-name: github.com/hashicorp/terraform-plugin-go 32 | # terraform-plugin-log should only be updated via terraform-plugin-framework 33 | - dependency-name: github.com/hashicorp/terraform-plugin-log 34 | # go-hclog should only be updated via terraform-plugin-log 35 | - dependency-name: github.com/hashicorp/go-hclog 36 | # grpc should only be updated via terraform-plugin-go/terraform-plugin-framework 37 | - dependency-name: google.golang.org/grpc 38 | # protobuf should only be updated via terraform-plugin-go/terraform-plugin-framework 39 | - dependency-name: google.golang.org/protobuf 40 | schedule: 41 | interval: weekly 42 | open-pull-requests-limit: 30 43 | labels: 44 | - chore 45 | - dependencies 46 | commit-message: 47 | prefix: "chore(deps)" 48 | include: "scope" -------------------------------------------------------------------------------- /.github/labeler-issues.yml: -------------------------------------------------------------------------------- 1 | --- 2 | bug: 3 | - 'panic:' 4 | crash: 5 | - 'panic:' 6 | -------------------------------------------------------------------------------- /.github/labeler-pull-requests.yml: -------------------------------------------------------------------------------- 1 | --- 2 | chore: 3 | - changed-files: 4 | - any-glob-to-any-file: 5 | - ".github/**/*" 6 | - ".gitignore" 7 | - "scripts/**/*" 8 | - "go.mod" 9 | - "go.sum" 10 | dependencies: 11 | - changed-files: 12 | - any-glob-to-any-file: 13 | - "go.mod" 14 | - "go.sum" 15 | documentation: 16 | - changed-files: 17 | - any-glob-to-any-file: 18 | - "**/*.md" 19 | - "docs/**/*" 20 | - "examples/**/*" 21 | github-actions: 22 | - changed-files: 23 | - any-glob-to-any-file: 24 | - ".github/workflows/**/*" 25 | needs-review: 26 | - changed-files: 27 | - any-glob-to-any-file: 28 | - "**" 29 | provider: 30 | - changed-files: 31 | - any-glob-to-any-file: 32 | - "vsphere/**/*" 33 | - "GNUmakefile" 34 | - "go.mod" 35 | - "go.sum" 36 | - "main.go" 37 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | ### Summary 19 | 20 | 23 | 24 | ### Type 25 | 26 | 29 | 30 | - [ ] `fix`: Bug Fix 31 | - [ ] `feat`: Feature or Enhancement 32 | - [ ] `docs`: Documentation 33 | - [ ] `refactor`: Refactoring 34 | - [ ] `chore`: Build, Dependencies, Workflows, etc. 35 | - [ ] `other`: Other (Please describe.) 36 | 37 | ### Breaking Changes? 38 | 39 | 43 | 44 | - [ ] Yes, there are breaking changes. 45 | - [ ] No, there are no breaking changes. 46 | 47 | ### Tests 48 | 49 | 53 | 54 | - [ ] Tests have been added or updated. 55 | - [ ] Tests have been completed. 56 | 57 | Output: 58 | 59 | 62 | 63 | ### Documentation 64 | 65 | 69 | 70 | - [ ] Documentation has been added or updated. 71 | 72 | ### Issue References 73 | 74 | 88 | 89 | ### Release Note 90 | 91 | 100 | 101 | ### Additional Information 102 | 103 | 106 | -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Check Documentation 3 | 4 | on: 5 | pull_request: 6 | 7 | permissions: 8 | contents: read 9 | 10 | jobs: 11 | docs: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 16 | - name: Install Tools 17 | run: make tools 18 | - name: Check Structure 19 | run: make docs-check 20 | - name: Check HCL Formatting 21 | run: make docs-hcl-lint 22 | -------------------------------------------------------------------------------- /.github/workflows/golangci-lint.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: golangci-lint 3 | 4 | on: 5 | pull_request: 6 | paths-ignore: 7 | - '.github/**' 8 | - '/*' 9 | - '!main.go' 10 | - 'acctests/**' 11 | - 'docs/**' 12 | - README.md 13 | - 'scripts/**' 14 | push: 15 | paths-ignore: 16 | - '.github/**' 17 | - '/*' 18 | - '!main.go' 19 | - 'acctests/**' 20 | - 'docs/**' 21 | - README.md 22 | - 'scripts/**' 23 | schedule: 24 | - cron: 30 00 * * 1 25 | 26 | permissions: 27 | contents: read 28 | 29 | jobs: 30 | lint: 31 | runs-on: ubuntu-latest 32 | timeout-minutes: 5 33 | steps: 34 | - name: Checkout 35 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 36 | - name: Setup Go 37 | uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 38 | with: 39 | go-version-file: go.mod 40 | cache: true 41 | - run: go mod download 42 | - run: go build -v . 43 | - name: Run Linters 44 | uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 45 | with: 46 | version: latest 47 | -------------------------------------------------------------------------------- /.github/workflows/issue-comment-created.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Issue Comment 3 | 4 | on: 5 | issue_comment: 6 | types: [created] 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | remove-if-waiting: 13 | uses: ./.github/workflows/remove-issue-label.yml 14 | permissions: 15 | contents: read 16 | issues: write 17 | with: 18 | label-name: "awaiting-response" 19 | remove-if-stale: 20 | uses: ./.github/workflows/remove-issue-label.yml 21 | permissions: 22 | contents: read 23 | issues: write 24 | with: 25 | label-name: "stale" 26 | -------------------------------------------------------------------------------- /.github/workflows/issue-opened.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Issue Labels 3 | 4 | on: 5 | issues: 6 | types: 7 | - opened 8 | 9 | permissions: 10 | contents: read 11 | 12 | jobs: 13 | label-issues: 14 | runs-on: ubuntu-latest 15 | permissions: 16 | issues: write 17 | steps: 18 | - name: Apply Labels 19 | uses: github/issue-labeler@c1b0f9f52a63158c4adc09425e858e87b32e9685 # v3.4 20 | with: 21 | repo-token: '${{ secrets.GITHUB_TOKEN }}' 22 | configuration-path: .github/labeler-issues.yml 23 | enable-versioned-regex: 0 24 | include-title: 1 25 | -------------------------------------------------------------------------------- /.github/workflows/lock.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Lock 3 | 4 | on: 5 | schedule: 6 | - cron: 30 00 * * * 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | lock: 13 | runs-on: ubuntu-latest 14 | permissions: 15 | issues: write 16 | pull-requests: write 17 | steps: 18 | - uses: dessant/lock-threads@1bf7ec25051fe7c00bdd17e6a7cf3d7bfb7dc771 # v5.0.1 19 | with: 20 | github-token: '${{ secrets.GITHUB_TOKEN }}' 21 | issue-comment: > 22 | I'm going to lock this issue because it has been closed for 30 23 | days. This helps our maintainers find and focus on the active 24 | issues. 25 | 26 | 27 | If you have found a problem that seems similar to this, 28 | please open a new issue and complete the issue template so we can 29 | capture all the details necessary to investigate further. 30 | issue-inactive-days: '30' 31 | pr-comment: > 32 | I'm going to lock this pull request because it has been closed for 33 | 30 days. This helps our maintainers find and focus on the active 34 | issues. 35 | 36 | 37 | If you have found a problem that seems related to this 38 | change, please open a new issue and complete the issue template so 39 | we can capture all the details necessary to investigate further. 40 | pr-inactive-days: '30' 41 | -------------------------------------------------------------------------------- /.github/workflows/milestone-closed.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Closed Milestones 3 | 4 | on: 5 | milestone: 6 | types: [closed] 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | comment: 13 | runs-on: ubuntu-latest 14 | permissions: 15 | issues: write 16 | pull-requests: write 17 | steps: 18 | - uses: bflad/action-milestone-comment@4618cbf8bf938d31af1c576beeaaa77f486f5af3 # v2.0.0 19 | with: 20 | body: | 21 | This functionality has been released in [${{ github.event.milestone.title }} of the Terraform Provider](https://github.com/${{ github.repository }}/blob/${{ github.event.milestone.title }}/CHANGELOG.md). Please see the [Terraform documentation on provider versioning](https://developer.hashicorp.com/terraform/language/providers/configuration#provider-versions) or reach out if you need any assistance upgrading. 22 | 23 | For further feature requests or bug reports with this functionality, please create a [new GitHub issue](https://github.com/${{ github.repository }}/issues/new/choose) following the template. Thank you! 24 | -------------------------------------------------------------------------------- /.github/workflows/pull-requests.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Pull Request Labels 3 | 4 | on: 5 | pull_request_target: 6 | branches: 7 | - main 8 | types: 9 | - opened 10 | - synchronize 11 | - reopened 12 | - edited 13 | - ready_for_review 14 | 15 | permissions: 16 | contents: read 17 | 18 | jobs: 19 | labeler: 20 | name: Labeler 21 | runs-on: ubuntu-latest 22 | permissions: 23 | contents: read 24 | pull-requests: write 25 | steps: 26 | - name: Apply Standard Labels 27 | uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5.0.0 28 | with: 29 | configuration-path: .github/labeler-pull-requests.yml 30 | repo-token: "${{ secrets.GITHUB_TOKEN }}" 31 | - name: Apply Sizing Labels 32 | uses: CodelyTV/pr-size-labeler@4ec67706cd878fbc1c8db0a5dcd28b6bb412e85a # v1.10.3 33 | with: 34 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 35 | xs_label: 'size/XS' 36 | xs_max_size: '30' 37 | s_label: 'size/S' 38 | s_max_size: '60' 39 | m_label: 'size/M' 40 | m_max_size: '150' 41 | l_label: 'size/L' 42 | l_max_size: '300' 43 | xl_label: 'size/XL' 44 | message_if_xl: '' 45 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Release 3 | 4 | on: 5 | push: 6 | tags: 7 | - 'v*.*.*' # Match stable releases. e.g. v1.2.3 8 | - 'v*.*.*-*' # Match pre-releases. e.g. v1.2.3-beta, v1.2.3-rc.1 9 | workflow_dispatch: 10 | 11 | permissions: 12 | contents: read 13 | 14 | jobs: 15 | release: 16 | runs-on: ubuntu-latest 17 | permissions: 18 | contents: write 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 22 | with: 23 | fetch-depth: 0 24 | - name: Setup Go 25 | uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 26 | with: 27 | go-version-file: go.mod 28 | - name: Import GPG Key 29 | id: import_gpg 30 | uses: crazy-max/ghaction-import-gpg@e89d40939c28e39f97cf32126055eeae86ba74ec # v6.3.0 31 | with: 32 | gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} 33 | passphrase: ${{ secrets.GPG_PASSPHRASE }} 34 | - name: Run GoReleaser 35 | uses: goreleaser/goreleaser-action@9c156ee8a17a598857849441385a2041ef570552 # v6.3.0 36 | with: 37 | version: latest 38 | args: release --clean --timeout 60m --release-header-tmpl .goreleaser.tmpl 39 | env: 40 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 41 | GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} 42 | -------------------------------------------------------------------------------- /.github/workflows/remove-issue-label.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Remove Labels 3 | 4 | on: 5 | # This file is reused, and called from other workflows. 6 | workflow_call: 7 | inputs: 8 | label-name: 9 | required: true 10 | type: string 11 | 12 | permissions: 13 | contents: read 14 | 15 | jobs: 16 | remove-label: 17 | runs-on: ubuntu-latest 18 | permissions: 19 | issues: write 20 | steps: 21 | - uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 22 | env: 23 | REMOVE_LABEL: ${{ inputs.label-name }} 24 | with: 25 | script: | 26 | const { REMOVE_LABEL } = process.env 27 | console.log(`Attempting to remove label "${REMOVE_LABEL}"`) 28 | 29 | const { data } = await github.rest.issues.listLabelsOnIssue({ 30 | issue_number: context.issue.number, 31 | owner: context.repo.owner, 32 | repo: context.repo.repo, 33 | }) 34 | 35 | // Return early if there are no labels 36 | if (data.length == 0){ 37 | console.log(`Issue has no labels; not attempting to remove label "${REMOVE_LABEL}"`) 38 | return 39 | } 40 | 41 | // Check if REMOVE_LABEL is present 42 | const filteredData = data.filter(label => label.name == REMOVE_LABEL) 43 | 44 | // Return early if filtering didn't identify the label as present 45 | if (filteredData.length == 0){ 46 | console.log(`Label "${REMOVE_LABEL}" not found on issue; not attempting to remove it.`) 47 | return 48 | } 49 | 50 | console.log(`Label "${REMOVE_LABEL}" found! Now deleting it from the issue...`) 51 | 52 | await github.rest.issues.removeLabel({ 53 | issue_number: context.issue.number, 54 | owner: context.repo.owner, 55 | repo: context.repo.repo, 56 | name: REMOVE_LABEL 57 | }) 58 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Stale 3 | 4 | on: 5 | schedule: 6 | - cron: 00 00 * * * 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | stale: 13 | runs-on: ubuntu-latest 14 | permissions: 15 | issues: write 16 | pull-requests: write 17 | steps: 18 | - uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0 19 | with: 20 | repo-token: ${{ secrets.GITHUB_TOKEN }} 21 | days-before-stale: 360 22 | days-before-close: 30 23 | exempt-issue-labels: needs-triage 24 | exempt-pr-labels: needs-review 25 | remove-stale-when-updated: true 26 | delete-branch: false 27 | stale-issue-label: stale 28 | stale-issue-message: > 29 | 'Marking this issue as stale due to inactivity. This helps us focus 30 | on the active issues. If this issue receives no comments in the next 31 | 30 days it will automatically be closed. 32 | 33 | 34 | If this issue was automatically closed and you feel this issue 35 | should be reopened, we encourage creating a new issue linking back 36 | to this one for added context. 37 | 38 | Thank you!' 39 | stale-pr-label: stale 40 | stale-pr-message: > 41 | 'Marking this pull request as stale due to inactivity. This helps us 42 | focus on the active pull requests. If this pull request receives no 43 | comments in the next 30 days it will automatically be closed. 44 | 45 | If this pull request was automatically closed and you feel this pull 46 | request should be reopened, we encourage creating a new pull request 47 | linking back to this one for added context. 48 | 49 | Thank you!' 50 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Tests 3 | 4 | on: 5 | push: 6 | paths: 7 | - '**.go' 8 | pull_request: 9 | branches: 10 | - main 11 | paths: 12 | - '**.go' 13 | schedule: 14 | - cron: 30 00 * * 1 15 | 16 | permissions: 17 | contents: read 18 | 19 | jobs: 20 | tests: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 25 | - name: Setup Go 26 | uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 27 | with: 28 | go-version-file: go.mod 29 | - name: Verify Go Modules 30 | run: go mod verify 31 | - name: Run Tests 32 | run: make test 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore any built binaries. 2 | terraform-provider-vsphere* 3 | 4 | # Ignore distribution directories. 5 | dist/ 6 | 7 | # Ignore editor directories. 8 | .idea/ 9 | .vscode/ 10 | 11 | # Ignore temporary editor files. 12 | *~ 13 | 14 | # Ignore macOS desktop services store files. 15 | **/.DS_Store 16 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: "2" 3 | 4 | output: 5 | formats: 6 | text: 7 | path: stdout 8 | 9 | linters: 10 | default: none 11 | enable: 12 | - errcheck 13 | - gosec 14 | - govet 15 | - ineffassign 16 | - misspell 17 | - revive 18 | - staticcheck 19 | - unconvert 20 | - unused 21 | exclusions: 22 | generated: lax 23 | presets: 24 | - comments 25 | - common-false-positives 26 | - legacy 27 | - std-error-handling 28 | rules: 29 | # Exclude specific rules to suppress false positives or acceptable issues. 30 | - linters: 31 | - gosec 32 | text: G115|G401 33 | - linters: 34 | - staticcheck 35 | text: SA1019 36 | paths: 37 | - third_party$ 38 | - builtin$ 39 | - examples$ 40 | 41 | issues: 42 | max-same-issues: 0 43 | 44 | formatters: 45 | enable: 46 | - gofmt 47 | - goimports 48 | exclusions: 49 | generated: lax 50 | paths: 51 | - third_party$ 52 | - builtin$ 53 | - examples$ 54 | -------------------------------------------------------------------------------- /.goreleaser.tmpl: -------------------------------------------------------------------------------- 1 | # Release {{ .Version }} ({{ .Date }}) 2 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | 4 | project_name: terraform-provider-vsphere 5 | 6 | before: 7 | hooks: 8 | - go mod tidy 9 | 10 | builds: 11 | - id: default 12 | binary: '{{ .ProjectName }}_v{{ .Version }}' 13 | goos: 14 | - linux 15 | - windows 16 | - darwin 17 | - freebsd 18 | goarch: 19 | - amd64 20 | - '386' 21 | - arm 22 | - arm64 23 | ignore: 24 | - goos: darwin 25 | goarch: '386' 26 | env: 27 | - CGO_ENABLED=0 28 | flags: 29 | - -trimpath 30 | ldflags: 31 | - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' 32 | mod_timestamp: '{{ .CommitTimestamp }}' 33 | 34 | archives: 35 | - id: default 36 | name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' 37 | formats: ['zip'] 38 | 39 | checksum: 40 | name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' 41 | algorithm: sha256 42 | extra_files: 43 | - glob: 'terraform-registry-manifest.json' 44 | name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json' 45 | 46 | signs: 47 | - id: default 48 | artifacts: checksum 49 | args: 50 | - "--batch" 51 | - "--local-user" 52 | - "{{ .Env.GPG_FINGERPRINT }}" 53 | - "--output" 54 | - "${signature}" 55 | - "--detach-sign" 56 | - "${artifact}" 57 | 58 | release: 59 | extra_files: 60 | - glob: 'terraform-registry-manifest.json' 61 | name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json' 62 | 63 | changelog: 64 | disable: true 65 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @vmware/terraform-provider-vsphere-maintainers 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export GOPATH_BIN := $(shell go env GOPATH)/bin 2 | export PATH := $(GOPATH_BIN):$(PATH) 3 | 4 | TEST?=$$(go list ./... |grep -v 'vendor') 5 | GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) 6 | PKG_NAME=vsphere 7 | 8 | default: build 9 | 10 | build: fmtcheck 11 | go install 12 | 13 | test: fmtcheck 14 | go test $(TEST) || exit 1 15 | echo $(TEST) | \ 16 | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 17 | 18 | testacc: fmtcheck 19 | TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 360m 20 | 21 | fmt: 22 | gofmt -w $(GOFMT_FILES) 23 | 24 | fmtcheck: 25 | @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" 26 | 27 | test-compile: 28 | @if [ "$(TEST)" = "./..." ]; then \ 29 | echo "ERROR: Set TEST to a specific package. For example,"; \ 30 | echo " make test-compile TEST=./$(PKG_NAME)"; \ 31 | exit 1; \ 32 | fi 33 | go test -c $(TEST) $(TESTARGS) 34 | 35 | tools: 36 | go install -mod=mod github.com/katbyte/terrafmt 37 | 38 | docs-check: 39 | @echo "==> Checking structure..." 40 | @sh -c "'$(CURDIR)/scripts/docscheck.sh'" 41 | 42 | docs-hcl-lint: tools 43 | @echo "==> Checking HCL formatting..." 44 | @$(GOPATH_BIN)/terrafmt diff ./docs --check --pattern '*.md' --quiet || (echo; echo "Unexpected HCL differences. Run 'make docs-hcl-fix'."; exit 1) 45 | 46 | docs-hcl-fix: tools 47 | @echo "==> Applying HCL formatting..." 48 | @$(GOPATH_BIN)/terrafmt fmt ./docs --pattern '*.md' 49 | 50 | .PHONY: build test testacc fmt fmtcheck test-compile tools docs-check docs-hcl-lint docs-hcl-fix 51 | 52 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | © Broadcom. All Rights Reserved. 2 | The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 | 4 | This product is licensed to you under the Mozilla Public License, V2.0 (the "License"). You may not use this product except in compliance with the License. 5 | 6 | This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file. 7 | -------------------------------------------------------------------------------- /docs/build.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | VMware vSphere 10 | 11 | # Building the Terraform Provider for VMware vSphere 12 | 13 | The instructions outlined below are specific to macOS and Linux only. 14 | 15 | If you wish to work on the provider, you'll first need [Go][golang-install] installed on your 16 | machine. Check the [requirements][requirements] before proceeding. 17 | 18 | 1. Clone the repository to: `$GOPATH/src/github.com/vmware/terraform-provider-vsphere` 19 | 20 | ```sh 21 | mkdir -p $GOPATH/src/github.com/vmware 22 | cd $GOPATH/src/github.com/vmware 23 | git clone git@github.com:vmware/terraform-provider-vsphere.git 24 | ``` 25 | 26 | 2. Enter the provider directory to build the provider. 27 | 28 | ```sh 29 | cd $GOPATH/src/github.com/vmware/terraform-provider-vsphere 30 | go get 31 | go build -o terraform-provider-vsphere 32 | ``` 33 | 34 | 3. Add the following to your `~/.terraformrc`: 35 | 36 | ```hcl 37 | provider_installation { 38 | dev_overrides { 39 | "vmware/vsphere" = "/Users/example/go/bin" 40 | } 41 | 42 | direct {} 43 | } 44 | ``` 45 | 46 | Where `/Users/example/go/bin` is your `GOPATH/bin` path. 47 | 48 | 4. Run `go install` to install the development binary. 49 | 50 | [golang-install]: https://golang.org/doc/install 51 | [requirements]: https://github.com/vmware/terraform-provider-vsphere#requirements 52 | -------------------------------------------------------------------------------- /docs/data-sources/compute_cluster.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_compute_cluster" 4 | sidebar_current: "docs-vsphere-data-source-compute-cluster" 5 | description: |- 6 | Provides a vSphere cluster data source. This can be used to get the general 7 | attributes of a vSphere cluster. 8 | --- 9 | 10 | # vsphere_compute_cluster 11 | 12 | The `vsphere_compute_cluster` data source can be used to discover the ID of a 13 | cluster in vSphere. This is useful to fetch the ID of a cluster that you want to 14 | use for virtual machine placement via the 15 | [`vsphere_virtual_machine`][docs-virtual-machine-resource] resource, allowing to 16 | specify the cluster's root resource pool directly versus using the alias 17 | available through the [`vsphere_resource_pool`][docs-resource-pool-data-source] 18 | data source. 19 | 20 | [docs-virtual-machine-resource]: /docs/providers/vsphere/r/virtual_machine.html 21 | [docs-resource-pool-data-source]: /docs/providers/vsphere/d/resource_pool.html 22 | 23 | -> You may also wish to see the [`vsphere_compute_cluster`][docs-compute-cluster-resource] 24 | resource for more information about clusters and how to managed the resource 25 | in Terraform. 26 | 27 | [docs-compute-cluster-resource]: /docs/providers/vsphere/r/compute_cluster.html 28 | 29 | ## Example Usage 30 | 31 | ```hcl 32 | data "vsphere_datacenter" "datacenter" { 33 | name = "dc-01" 34 | } 35 | 36 | data "vsphere_compute_cluster" "compute_cluster" { 37 | name = "cluster-01" 38 | datacenter_id = data.vsphere_datacenter.datacenter.id 39 | } 40 | ``` 41 | 42 | ## Argument Reference 43 | 44 | The following arguments are supported: 45 | 46 | * `name` - (Required) The name or absolute path to the cluster. 47 | * `datacenter_id` - (Optional) The 48 | [managed object reference ID][docs-about-morefs] of the datacenter the cluster 49 | is located in. This can be omitted if the search path used in `name` is an 50 | absolute path. For default datacenters, use the `id` attribute from an empty 51 | `vsphere_datacenter` data source. 52 | 53 | ## Attribute Reference 54 | 55 | The following attributes are exported: 56 | 57 | * `id`: The [managed object reference ID][docs-about-morefs] of the cluster. 58 | * `resource_pool_id`: The [managed object reference ID][docs-about-morefs] of 59 | the root resource pool for the cluster. 60 | 61 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 62 | -------------------------------------------------------------------------------- /docs/data-sources/compute_cluster_host_group.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_compute_cluster_host_group" 4 | sidebar_current: "docs-vsphere-data-source-compute-cluster-host-group" 5 | description: |- 6 | Provides a vSphere cluster host group data source. Returns attributes of a 7 | vSphere cluster host group. 8 | --- 9 | 10 | # vsphere_compute_cluster_host_group 11 | 12 | The `vsphere_compute_cluster_host_group` data source can be used to discover 13 | the IDs ESXi hosts in a host group and return host group attributes to other 14 | resources. 15 | 16 | ## Example Usage 17 | 18 | ```hcl 19 | data "vsphere_datacenter" "datacenter" { 20 | name = "dc-01" 21 | } 22 | 23 | data "vsphere_compute_cluster" "cluster" { 24 | name = "cluster-01" 25 | datacenter_id = data.vsphere_datacenter.datacenter.id 26 | } 27 | 28 | data "vsphere_compute_cluster_host_group" "host_group" { 29 | name = "hostgroup-01" 30 | compute_cluster_id = data.vsphere_compute_cluster.cluster.id 31 | } 32 | 33 | resource "vsphere_compute_cluster_vm_host_rule" "host_rule" { 34 | compute_cluster_id = data.vsphere_compute_cluster.cluster.id 35 | name = "terraform-host-rule1" 36 | vm_group_name = "vmgroup-01" 37 | affinity_host_group_name = data.vsphere_compute_cluster_host_group.host_group.name 38 | } 39 | ``` 40 | 41 | ## Argument Reference 42 | 43 | The following arguments are supported: 44 | 45 | * `name` - (Required) The name of the host group. 46 | * `compute_cluster_id` - (Required) The 47 | [managed object reference ID][docs-about-morefs] of the compute cluster for 48 | the host group. 49 | 50 | ## Attribute Reference 51 | 52 | * `host_system_ids`: The [managed object reference ID][docs-about-morefs] of 53 | the ESXi hosts in the host group. 54 | 55 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 56 | -------------------------------------------------------------------------------- /docs/data-sources/content_library.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Virtual Machine" 3 | page_title: "VMware vSphere: vsphere_content_library" 4 | sidebar_current: "docs-vsphere-data-source-content-library" 5 | description: |- 6 | Provides a VMware vSphere content library data source. 7 | --- 8 | 9 | # vsphere_content_library 10 | 11 | The `vsphere_content_library` data source can be used to discover the ID of a 12 | content library. 13 | 14 | ~> **NOTE:** This resource requires vCenter and is not available on direct ESXi 15 | host connections. 16 | 17 | ## Example Usage 18 | 19 | ```hcl 20 | data "vsphere_content_library" "content_library" { 21 | name = "Content Library" 22 | } 23 | ``` 24 | 25 | ## Argument Reference 26 | 27 | The following arguments are supported: 28 | 29 | * `name` - (Required) The name of the content library. 30 | 31 | ## Attribute Reference 32 | 33 | The only attribute this resource exports is the `id` of the resource, which is 34 | a combination of the [managed object reference ID][docs-about-morefs] of the 35 | cluster, and the name of the virtual machine group. 36 | 37 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 38 | -------------------------------------------------------------------------------- /docs/data-sources/content_library_item.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Virtual Machine" 3 | page_title: "VMware vSphere: vsphere_content_library_item" 4 | sidebar_current: "docs-vsphere-data-source-content-library-item" 5 | description: |- 6 | Provides a VMware vSphere content library item data source. 7 | --- 8 | 9 | # vsphere_content_library_item 10 | 11 | The `vsphere_content_library_item` data source can be used to discover the ID 12 | of a content library item. 13 | 14 | ~> **NOTE:** This resource requires vCenter and is not available on direct ESXi 15 | host connections. 16 | 17 | ## Example Usage 18 | 19 | ```hcl 20 | data "vsphere_content_library" "library" { 21 | name = "Content Library" 22 | } 23 | 24 | data "vsphere_content_library_item" "item" { 25 | name = "ovf-ubuntu-server-lts" 26 | type = "ovf" 27 | library_id = data.vsphere_content_library.library.id 28 | } 29 | 30 | data "vsphere_content_library_item" "item" { 31 | name = "tpl-ubuntu-server-lts" 32 | type = "vm-template" 33 | library_id = data.vsphere_content_library.library.id 34 | } 35 | 36 | data "vsphere_content_library_item" "item" { 37 | name = "iso-ubuntu-server-lts" 38 | type = "iso" 39 | library_id = data.vsphere_content_library.library.id 40 | } 41 | ``` 42 | 43 | ## Argument Reference 44 | 45 | The following arguments are supported: 46 | 47 | * `name` - (Required) The name of the content library item. 48 | * `library_id` - (Required) The ID of the content library in which the item 49 | exists. 50 | * `type` - (Required) The type for the content library item. One of `ovf`, 51 | `vm-template`, or `iso` 52 | 53 | ## Attribute Reference 54 | 55 | * `id` - The UUID of the content library item. 56 | -------------------------------------------------------------------------------- /docs/data-sources/custom_attribute.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_custom_attribute" 4 | sidebar_current: "docs-vsphere-data-source-custom-attribute" 5 | description: |- 6 | Provides a vSphere custom attribute data source. This can be used to 7 | reference custom attributes not managed in Terraform. 8 | --- 9 | 10 | # vsphere_custom_attribute 11 | 12 | The `vsphere_custom_attribute` data source can be used to reference custom 13 | attributes that are not managed by Terraform. Its attributes are exactly the 14 | same as the [`vsphere_custom_attribute` resource][resource-custom-attribute], 15 | and, like importing, the data source takes a name argument for the search. The 16 | `id` and other attributes are then populated with the data found by the search. 17 | 18 | [resource-custom-attribute]: /docs/providers/vsphere/r/custom_attribute.html 19 | 20 | ~> **NOTE:** Custom attributes are unsupported on direct ESXi host connections 21 | and require vCenter Server. 22 | 23 | ## Example Usage 24 | 25 | ```hcl 26 | data "vsphere_custom_attribute" "attribute" { 27 | name = "terraform-test-attribute" 28 | } 29 | ``` 30 | 31 | ## Argument Reference 32 | 33 | * `name` - (Required) The name of the custom attribute. 34 | 35 | ## Attribute Reference 36 | 37 | In addition to the `id` being exported, all of the fields that are available in 38 | the [`vsphere_custom_attribute` resource][resource-custom-attribute] are also 39 | populated. 40 | -------------------------------------------------------------------------------- /docs/data-sources/datacenter.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_datacenter" 4 | sidebar_current: "docs-vsphere-data-source-datacenter" 5 | description: |- 6 | Provides a data source to return the ID of a vSphere datacenter object. 7 | --- 8 | 9 | # vsphere_datacenter 10 | 11 | The `vsphere_datacenter` data source can be used to discover the ID of a vSphere 12 | datacenter object. This can then be used with resources or data sources that 13 | require a datacenter, such as the [`vsphere_host`][data-source-vsphere-host] 14 | data source. 15 | 16 | [data-source-vsphere-host]: /docs/providers/vsphere/d/host.html 17 | 18 | ## Example Usage 19 | 20 | ```hcl 21 | data "vsphere_datacenter" "datacenter" { 22 | name = "dc-01" 23 | } 24 | ``` 25 | 26 | ## Argument Reference 27 | 28 | The following arguments are supported: 29 | 30 | * `name` - (Optional) The name of the datacenter. This can be a name or path. 31 | Can be omitted if there is only one datacenter in the inventory. 32 | 33 | ~> **NOTE:** When used with an ESXi host, this data source _always_ returns the 34 | host's "default" datacenter, which is a special datacenter name unrelated to the 35 | datacenters that exist in the vSphere inventory when managed by a vCenter Server 36 | instance. Hence, the `name` attribute is completely ignored. 37 | 38 | ## Attribute Reference 39 | 40 | The following attributes are exported: 41 | 42 | * `id` - The [managed objectID][docs-about-morefs] of the vSphere datacenter object. 43 | * `virtual_machines` - List of all virtual machines included in the vSphere datacenter object. 44 | 45 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 46 | -------------------------------------------------------------------------------- /docs/data-sources/datastore.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Storage" 3 | page_title: "VMware vSphere: vsphere_datastore" 4 | sidebar_current: "docs-vsphere-data-source-datastore" 5 | description: |- 6 | Provides a data source to return the ID of a vSphere datastore object. 7 | --- 8 | 9 | # vsphere_datastore 10 | 11 | The `vsphere_datastore` data source can be used to discover the ID of a vSphere 12 | datastore object. This can then be used with resources or data sources that 13 | require a datastore. For example, to create virtual machines in using the 14 | [`vsphere_virtual_machine`][docs-virtual-machine-resource] resource. 15 | 16 | [docs-virtual-machine-resource]: /docs/providers/vsphere/r/virtual_machine.html 17 | 18 | ## Example Usage 19 | 20 | ```hcl 21 | data "vsphere_datacenter" "datacenter" { 22 | name = "dc-01" 23 | } 24 | 25 | data "vsphere_datastore" "datastore" { 26 | name = "datastore-01" 27 | datacenter_id = data.vsphere_datacenter.datacenter.id 28 | } 29 | ``` 30 | 31 | ## Argument Reference 32 | 33 | The following arguments are supported: 34 | 35 | - `name` - (Required) The name of the datastore. This can be a name or path. 36 | - `datacenter_id` - (Optional) The 37 | [managed object reference ID][docs-about-morefs] of the datacenter the 38 | datastore is located in. This can be omitted if the search path used in `name` 39 | is an absolute path. For default datacenters, use the `id` attribute from an 40 | empty `vsphere_datacenter` data source. 41 | 42 | ## Attribute Reference 43 | 44 | The following attributes are exported: 45 | 46 | - `id` - The ID of the datastore. 47 | - `stats` - The disk space usage statistics for the specific datastore. The 48 | total datastore capacity is represented as `capacity` and the free remaining 49 | disk is represented as `free`. 50 | 51 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 52 | -------------------------------------------------------------------------------- /docs/data-sources/datastore_cluster.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Storage" 3 | page_title: "VMware vSphere: vsphere_datastore_cluster" 4 | sidebar_current: "docs-vsphere-data-source-cluster-datastore" 5 | description: |- 6 | Provides a data source to return the ID of a vSphere datastore cluster object. 7 | --- 8 | 9 | # vsphere_datastore_cluster 10 | 11 | The `vsphere_datastore_cluster` data source can be used to discover the ID of a 12 | vSphere datastore cluster object. This can then be used with resources or data 13 | sources that require a datastore. For example, to assign datastores using the 14 | [`vsphere_nas_datastore`][docs-nas-datastore-resource] or 15 | [`vsphere_vmfs_datastore`][docs-vmfs-datastore-resource] resources, or to create 16 | virtual machines in using the 17 | [`vsphere_virtual_machine`][docs-virtual-machine-resource] resource. 18 | 19 | [docs-nas-datastore-resource]: /docs/providers/vsphere/r/nas_datastore.html 20 | [docs-vmfs-datastore-resource]: /docs/providers/vsphere/r/vmfs_datastore.html 21 | [docs-virtual-machine-resource]: /docs/providers/vsphere/r/virtual_machine.html 22 | 23 | ## Example Usage 24 | 25 | ```hcl 26 | data "vsphere_datacenter" "datacenter" { 27 | name = "dc-01" 28 | } 29 | 30 | data "vsphere_datastore_cluster" "datastore_cluster" { 31 | name = "datastore-cluster-01" 32 | datacenter_id = data.vsphere_datacenter.datacenter.id 33 | } 34 | ``` 35 | 36 | ## Argument Reference 37 | 38 | The following arguments are supported: 39 | 40 | * `name` - (Required) The name or absolute path to the datastore cluster. 41 | * `datacenter_id` - (Optional) The [managed object reference 42 | ID][docs-about-morefs] of the datacenter the datastore cluster is located in. 43 | This can be omitted if the search path used in `name` is an absolute path. 44 | For default datacenters, use the id attribute from an empty 45 | `vsphere_datacenter` data source. 46 | 47 | ## Attribute Reference 48 | 49 | The following attributes are exported: 50 | 51 | * `id` - The [managed objectID][docs-about-morefs] of the vSphere datastore cluster object. 52 | 53 | * `datastores` - (Optional) The names of the datastores included in the specific 54 | cluster. 55 | 56 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 57 | -------------------------------------------------------------------------------- /docs/data-sources/datastore_stats.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Storage" 3 | page_title: "VMware vSphere: vsphere_datastore_stats" 4 | sidebar_current: "docs-vsphere-data-source-datastore-stats" 5 | description: |- 6 | Provides a data source to return the usage stats for all vSphere datastore 7 | objects in a datacenter. 8 | --- 9 | 10 | # vsphere_datastore_stats 11 | 12 | The `vsphere_datastore_stats` data source can be used to retrieve the usage 13 | stats of all vSphere datastore objects in a datacenter. This can then be used as 14 | a standalone data source to get information required as input to other data 15 | sources. 16 | 17 | ## Example Usage 18 | 19 | ```hcl 20 | data "vsphere_datacenter" "datacenter" { 21 | name = "dc-01" 22 | } 23 | 24 | data "vsphere_datastore_stats" "datastore_stats" { 25 | datacenter_id = data.vsphere_datacenter.datacenter.id 26 | } 27 | ``` 28 | 29 | A useful example of this data source would be to determine the datastore with 30 | the most free space. For example, in addition to the above: 31 | 32 | Create an `outputs.tf` like that: 33 | 34 | ```hcl 35 | output "max_free_space_name" { 36 | value = local.max_free_space_name 37 | } 38 | 39 | output "max_free_space" { 40 | value = local.max_free_space 41 | } 42 | ``` 43 | 44 | and a `locals.tf` like that: 45 | 46 | ```hcl 47 | locals { 48 | free_space_values = { for k, v in data.vsphere_datastore_stats.datastore_stats.free_space : k => tonumber(v) } 49 | filtered_values = { for k, v in local.free_space_values : k => tonumber(v) if v != null } 50 | numeric_values = [for v in values(local.filtered_values) : tonumber(v)] 51 | max_free_space = max(local.numeric_values...) 52 | max_free_space_name = [for k, v in local.filtered_values : k if v == local.max_free_space][0] 53 | } 54 | ``` 55 | 56 | This way you can get the storage object with the most space available. 57 | 58 | ## Argument Reference 59 | 60 | The following arguments are supported: 61 | 62 | - `datacenter_id` - (Required) The 63 | [managed object reference ID][docs-about-morefs] of the datacenter the 64 | datastores are located in. For default datacenters, use the `id` attribute 65 | from an empty `vsphere_datacenter` data source. 66 | 67 | ## Attribute Reference 68 | 69 | The following attributes are exported: 70 | 71 | - `datacenter_id` - The [managed object reference ID][docs-about-morefs] of the 72 | datacenter the datastores are located in. 73 | - `free_space` - A mapping of the free space for each datastore in the 74 | datacenter, where the name of the datastore is used as key and the free space 75 | as value. 76 | - `capacity` - A mapping of the capacity for all datastore in the datacenter, 77 | where the name of the datastore is used as key and the capacity as value. 78 | 79 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 80 | -------------------------------------------------------------------------------- /docs/data-sources/distributed_virtual_switch.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Networking" 3 | page_title: "VMware vSphere: vsphere_distributed_virtual_switch" 4 | sidebar_current: "docs-vsphere-data-source-distributed-virtual-switch" 5 | description: |- 6 | Provides a vSphere distributed switch data source. This can be used to get 7 | attributes of a the distributed switch. 8 | --- 9 | 10 | # vsphere_distributed_virtual_switch 11 | 12 | The `vsphere_distributed_virtual_switch` data source can be used to discover the 13 | ID and uplink data of a of a vSphere distributed switch (VDS). This can then be 14 | used with resources or data sources that require a VDS, such as the 15 | [`vsphere_distributed_port_group`][distributed-port-group] resource, for which 16 | an example is shown below. 17 | 18 | [distributed-port-group]: /docs/providers/vsphere/r/distributed_port_group.html 19 | 20 | ~> **NOTE:** This data source requires vCenter Server and is not available on 21 | direct ESXi host connections. 22 | 23 | ## Example Usage 24 | 25 | The following example locates a distributed switch named `vds-01`, in the 26 | datacenter `dc-01`. It then uses this distributed switch to set up a 27 | `vsphere_distributed_port_group` resource that uses the first uplink as a 28 | primary uplink and the second uplink as a secondary. 29 | 30 | ```hcl 31 | data "vsphere_datacenter" "datacenter" { 32 | name = "dc-01" 33 | } 34 | 35 | data "vsphere_distributed_virtual_switch" "vds" { 36 | name = "vds-01" 37 | datacenter_id = data.vsphere_datacenter.datacenter.id 38 | } 39 | 40 | resource "vsphere_distributed_port_group" "dvpg" { 41 | name = "dvpg-01" 42 | distributed_virtual_switch_uuid = data.vsphere_distributed_virtual_switch.vds.id 43 | 44 | active_uplinks = [data.vsphere_distributed_virtual_switch.vds.uplinks[0]] 45 | standby_uplinks = [data.vsphere_distributed_virtual_switch.vds.uplinks[1]] 46 | } 47 | ``` 48 | 49 | ## Argument Reference 50 | 51 | The following arguments are supported: 52 | 53 | * `name` - (Required) The name of the VDS. This can be a name or path. 54 | * `datacenter_id` - (Optional) The 55 | [managed object reference ID][docs-about-morefs] of the datacenter the VDS is 56 | located in. This can be omitted if the search path used in `name` is an 57 | absolute path. For default datacenters, use the `id` attribute from an empty 58 | `vsphere_datacenter` data source. 59 | 60 | ## Attribute Reference 61 | 62 | The following attributes are exported: 63 | 64 | * `id`: The UUID of the vSphere distributed switch. 65 | * `uplinks`: The list of the uplinks on this vSphere distributed switch, as per 66 | the [`uplinks`][distributed-virtual-switch-uplinks] argument to the 67 | [`vsphere_distributed_virtual_switch`][distributed-virtual-switch-resource] 68 | resource. 69 | 70 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 71 | [distributed-virtual-switch-uplinks]: /docs/providers/vsphere/r/distributed_virtual_switch.html#uplinks 72 | [distributed-virtual-switch-resource]: /docs/providers/vsphere/r/distributed_virtual_switch.html 73 | -------------------------------------------------------------------------------- /docs/data-sources/dynamic.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_dynamic" 4 | sidebar_current: "docs-vsphere-data-source-dynamic" 5 | description: |- 6 | A data source that can be used to get the [managed object reference ID 7 | of any tagged managed object in the vSphere inventory. 8 | --- 9 | 10 | # vsphere_dynamic 11 | 12 | The `vsphere_dynamic` data source can be used to get the 13 | [managed object reference ID][docs-about-morefs] of any tagged managed object in 14 | vCenter Server by providing a list of tag IDs and an optional regular expression 15 | to filter objects by name. 16 | 17 | ## Example Usage 18 | 19 | ```hcl 20 | data "vsphere_tag_category" "category" { 21 | name = "SomeCategory" 22 | } 23 | 24 | data "vsphere_tag" "tag1" { 25 | name = "FirstTag" 26 | category_id = data.vsphere_tag_category.cat.id 27 | } 28 | 29 | data "vsphere_tag" "tag2" { 30 | name = "SecondTag" 31 | category_id = data.vsphere_tag_category.cat.id 32 | } 33 | 34 | data "vsphere_dynamic" "dyn" { 35 | filter = [data.vsphere_tag.tag1.id, data.vsphere_tag.tag1.id] 36 | name_regex = "ubuntu" 37 | type = "Datacenter" 38 | } 39 | ``` 40 | 41 | ## Argument Reference 42 | 43 | The following arguments are supported: 44 | 45 | * `filter` - (Required) A list of tag IDs that must be present on an object to 46 | be a match. 47 | * `name_regex` - (Optional) A regular expression that will be used to match the 48 | object's name. 49 | * `type` - (Optional) The managed object type the returned object must match. 50 | The managed object types can be found in the managed object type section 51 | [here](https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/). 52 | 53 | ## Attribute Reference 54 | 55 | * `id` - The device ID of the matched managed object. 56 | 57 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 58 | -------------------------------------------------------------------------------- /docs/data-sources/guest_os_customization.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Virtual Machine" 3 | page_title: "VMware vSphere: vsphere_guest_os_customization" 4 | sidebar_current: "docs-vsphere-data-guest-os-customization" 5 | description: |- 6 | Provides a VMware vSphere guest customization spec data source. 7 | This can be used to apply the customization spec when virtual machine is 8 | cloned. 9 | --- 10 | 11 | # vsphere_guest_os_customization 12 | 13 | The `vsphere_guest_os_customization` data source can be used to discover the 14 | details about a customization specification for a guest operating system. 15 | 16 | ## Example Usage 17 | 18 | ```hcl 19 | data "vsphere_datacenter" "datacenter" { 20 | name = "dc-01" 21 | } 22 | 23 | data "vsphere_virtual_machine" "template" { 24 | name = "windows-template" 25 | datacenter_id = data.vsphere_datacenter.datacenter.id 26 | } 27 | 28 | data "vsphere_guest_os_customization" "windows" { 29 | name = "windows" 30 | } 31 | 32 | resource "vsphere_virtual_machine" "vm" { 33 | # ... other configuration ... 34 | template_uuid = data.vsphere_virtual_machine.template.id 35 | customization_spec { 36 | id = data.vsphere_guest_os_customization.windows.id 37 | } 38 | # ... other configuration ... 39 | } 40 | ``` 41 | 42 | ## Argument Reference 43 | 44 | The following arguments are supported: 45 | 46 | - `name` - (Required) The name of the customization specification is the unique 47 | identifier per vCenter Server instance. 48 | 49 | ## Attribute Reference 50 | 51 | - `type` - The type of customization specification: One among: Windows, Linux. 52 | - `description` - The description for the customization specification. 53 | - `last_update_time` - The time of last modification to the customization 54 | specification. 55 | - `change_version` - The number of last changed version to the customization 56 | specification. 57 | - `spec` - Container object for the guest operating system properties to be 58 | customized. See 59 | [virtual machine customizations](virtual_machine#virtual-machine-customizations) 60 | -------------------------------------------------------------------------------- /docs/data-sources/host.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_host" 4 | sidebar_current: "docs-vsphere-data-source-host" 5 | description: |- 6 | A data source that can be used to return the attributes of an ESXi host. 7 | --- 8 | 9 | # vsphere_host 10 | 11 | The `vsphere_host` data source can be used to discover the ID of an ESXi host. 12 | This can then be used with resources or data sources that require an ESX host's 13 | [managed object reference ID][docs-about-morefs]. 14 | 15 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 16 | 17 | ## Example Usage 18 | 19 | ```hcl 20 | data "vsphere_datacenter" "datacenter" { 21 | name = "dc-01" 22 | } 23 | 24 | data "vsphere_host" "host" { 25 | name = "esxi-01.example.com" 26 | datacenter_id = data.vsphere_datacenter.datacenter.id 27 | } 28 | ``` 29 | 30 | ## Argument Reference 31 | 32 | The following arguments are supported: 33 | 34 | * `datacenter_id` - (Required) The 35 | [managed object reference ID][docs-about-morefs] of a vSphere datacenter 36 | object. 37 | * `name` - (Optional) The name of the ESXI host. This can be a name or path. Can 38 | be omitted if there is only one host in your inventory. 39 | 40 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 41 | 42 | ~> **NOTE:** When used against an ESXi host directly, this data source _always_ 43 | returns the ESXi host's object ID, regardless of what is entered into `name`. 44 | 45 | ## Attribute Reference 46 | 47 | * `id` - The [managed objectID][docs-about-morefs] of the ESXi host. 48 | * `resource_pool_id` - The [managed object ID][docs-about-morefs] of the ESXi 49 | host's root resource pool. 50 | 51 | -> Note that the resource pool referenced by 52 | [`resource_pool_id`](#resource_pool_id) is dependent on the ESXi host's state. 53 | If it is a standalone ESXi host, the resource pool will belong to the host only; 54 | however, if it is a member of a cluster, the resource pool will be the root for 55 | the cluster. 56 | 57 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 58 | -------------------------------------------------------------------------------- /docs/data-sources/host_base_images.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Lifecycle" 3 | page_title: "VMware vSphere: vsphere_host_base_images" 4 | sidebar_current: "docs-vsphere-data-source-host-base-images" 5 | description: |- 6 | Provides a VMware vSphere ESXi base images data source. 7 | This can be used to get the list of ESXi base images available for cluster 8 | software management. 9 | --- 10 | 11 | # vsphere_host_base_images 12 | 13 | The `vsphere_host_base_images` data source can be used to get the list of ESXi 14 | base images available for cluster software management. 15 | 16 | ## Example Usage 17 | 18 | ```hcl 19 | data "vsphere_host_base_images" "base_images" {} 20 | ``` 21 | 22 | ## Attribute Reference 23 | 24 | The following attributes are exported: 25 | 26 | * `base_images` - List of available images. 27 | * `version` - The ESXi version identifier for the image 28 | -------------------------------------------------------------------------------- /docs/data-sources/host_pci_device.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_host_pci_device" 4 | sidebar_current: "docs-vsphere-data-source-host_pci_device" 5 | description: |- 6 | A data source that can be used to get information for a PCI passthrough 7 | device on an ESXi host. 8 | --- 9 | 10 | # vsphere_host_pci_device 11 | 12 | The `vsphere_host_pci_device` data source can be used to discover the device ID 13 | of a vSphere host's PCI device. This can then be used with 14 | `vsphere_virtual_machine`'s `pci_device_id`. 15 | 16 | ## Example Usage with Vendor ID and Class ID 17 | 18 | ```hcl 19 | data "vsphere_datacenter" "datacenter" { 20 | name = "dc-01" 21 | } 22 | 23 | data "vsphere_host" "host" { 24 | name = "esxi-01.example.com" 25 | datacenter_id = data.vsphere_datacenter.datacenter.id 26 | } 27 | 28 | data "vsphere_host_pci_device" "dev" { 29 | host_id = data.vsphere_host.host.id 30 | class_id = 123 31 | vendor_id = 456 32 | } 33 | ``` 34 | 35 | ## Example Usage with Name Regular Expression 36 | 37 | ```hcl 38 | data "vsphere_datacenter" "datacenter" { 39 | name = "dc-01" 40 | } 41 | 42 | data "vsphere_host" "host" { 43 | name = "esxi-01.example.com" 44 | datacenter_id = data.vsphere_datacenter.datacenter.id 45 | } 46 | 47 | data "vsphere_host_pci_device" "dev" { 48 | host_id = data.vsphere_host.host.id 49 | name_regex = "MMC" 50 | } 51 | ``` 52 | 53 | ## Argument Reference 54 | 55 | The following arguments are supported: 56 | 57 | * `host_id` - (Required) The [managed object reference ID][docs-about-morefs] of 58 | a host. 59 | * `name_regex` - (Optional) A regular expression that will be used to match the 60 | host PCI device name. 61 | * `vendor_id` - (Optional) The hexadecimal PCI device vendor ID. 62 | * `class_id` - (Optional) The hexadecimal PCI device class ID 63 | 64 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 65 | 66 | ~> **NOTE:** `name_regex`, `vendor_id`, and `class_id` can all be used together. 67 | 68 | ## Attribute Reference 69 | 70 | * `id` - The device ID of the PCI device. 71 | * `name` - The name of the PCI device. 72 | -------------------------------------------------------------------------------- /docs/data-sources/host_thumbprint.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_host_thumbprint" 4 | sidebar_current: "docs-vsphere-data-source-datacenter" 5 | description: |- 6 | A data source that can be used to get the thumbprint of an ESXi host. 7 | --- 8 | 9 | # vsphere_host_thumbprint 10 | 11 | The `vsphere_thumbprint` data source can be used to discover the host thumbprint 12 | of an ESXi host. This can be used when adding the `vsphere_host` resource to a 13 | cluster or a vCenter Server instance. 14 | 15 | * If the ESXi host is using a certificate chain, the first one returned will be 16 | used to generate the thumbprint. 17 | 18 | * If the ESXi host has a certificate issued by a certificate authority, ensure 19 | that the the certificate authority is trusted on the system running the plan. 20 | 21 | ## Example Usage 22 | 23 | ```hcl 24 | data "vsphere_host_thumbprint" "thumbprint" { 25 | address = "esxi-01.example.com" 26 | } 27 | ``` 28 | 29 | ## Argument Reference 30 | 31 | The following arguments are supported: 32 | 33 | * `address` - (Required) The address of the ESXi host to retrieve the thumbprint 34 | from. 35 | * `insecure` - (Optional) Disables SSL certificate verification. Default: `false` 36 | * `port` - (Optional) The port to use connecting to the ESXi host. Default: 443 37 | 38 | ## Attribute Reference 39 | 40 | The only exported attribute is `id`, which is the thumbprint of the ESXi host. 41 | -------------------------------------------------------------------------------- /docs/data-sources/host_vgpu_profile.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Host and Cluster Management" 3 | page_title: "VMware vSphere: vsphere_host_vgpu_profile" 4 | sidebar_current: "docs-vsphere-data-source-host_vgpu_profile" 5 | description: |- 6 | A data source that can be used to get information for one or all vGPU profiles 7 | available on an ESXi host. 8 | --- 9 | 10 | # vsphere_host_vgpu_profile 11 | 12 | The `vsphere_host_vgpu_profile` data source can be used to discover the 13 | available vGPU profiles of a vSphere host. 14 | 15 | ## Example Usage to return all vGPU profiles 16 | 17 | ```hcl 18 | data "vsphere_datacenter" "datacenter" { 19 | name = "dc-01" 20 | } 21 | 22 | data "vsphere_host" "host" { 23 | name = "esxi-01.example.com" 24 | datacenter_id = data.vsphere_datacenter.datacenter.id 25 | } 26 | 27 | data "vsphere_host_vgpu_profile" "vgpu_profile" { 28 | host_id = data.vsphere_host.host.id 29 | } 30 | ``` 31 | 32 | ## Example Usage with vGPU profile name_regex 33 | 34 | ```hcl 35 | data "vsphere_datacenter" "datacenter" { 36 | name = "dc-01" 37 | } 38 | 39 | data "vsphere_host" "host" { 40 | name = "esxi-01.example.com" 41 | datacenter_id = data.vsphere_datacenter.datacenter.id 42 | } 43 | 44 | data "vsphere_host_vgpu_profile" "vgpu_profile" { 45 | host_id = data.vsphere_host.host.id 46 | name_regex = "a100" 47 | } 48 | ``` 49 | 50 | ## Argument Reference 51 | 52 | The following arguments are supported: 53 | 54 | * `host_id` - (Required) The [managed object reference ID][docs-about-morefs] of 55 | a host. 56 | * `name_regex` - (Optional) A regular expression that will be used to match the 57 | host vGPU profile name. 58 | 59 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 60 | 61 | ## Attribute Reference 62 | 63 | The following attributes are exported: 64 | 65 | * `host_id` - The [managed objectID][docs-about-morefs] of the ESXi host. 66 | * `id` - Unique (SHA256) id based on the host_id if the ESXi host. 67 | * `name_regex` - (Optional) A regular expression that will be used to match the 68 | host vGPU profile name. 69 | * `vgpu_profiles` - The list of available vGPU profiles on the ESXi host. 70 | This may be and empty array if no vGPU profile are identified. 71 | * `vgpu` - Name of a particular vGPU available as a shared GPU device (vGPU 72 | profile). 73 | * `disk_snapshot_supported` - Indicates whether the GPU plugin on this host is 74 | capable of disk-only snapshots when VM is not powered off. 75 | * `memory_snapshot_supported` - Indicates whether the GPU plugin on this host 76 | is capable of memory snapshots. 77 | * `migrate_supported` - Indicates whether the GPU plugin on this host is 78 | capable of migration. 79 | * `suspend_supported` - Indicates whether the GPU plugin on this host is 80 | capable of suspend-resume. 81 | -------------------------------------------------------------------------------- /docs/data-sources/license.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Administration" 3 | page_title: "VMware vSphere: vsphere_license" 4 | sidebar_current: "docs-vsphere-data-source-admin-license" 5 | description: |- 6 | Provides a VMware vSphere license data source. 7 | --- 8 | 9 | # vsphere_license 10 | 11 | The `vsphere_license` data source can be used to get the general attributes of 12 | a license keys from a vCenter Server instance. 13 | 14 | ## Example Usage 15 | 16 | ```hcl 17 | data "vsphere_license" "license" { 18 | license_key = "00000-00000-00000-00000-00000" 19 | } 20 | ``` 21 | 22 | ## Argument Reference 23 | 24 | The following arguments are supported: 25 | 26 | * `license_key` - (Required) The license key value. 27 | 28 | ## Attribute Reference 29 | 30 | The following attributes are exported: 31 | 32 | * `id` - The license key ID. 33 | * `labels` - A map of labels applied to the license key. 34 | * `edition_key` - The product edition of the license key. 35 | * `name` - The display name for the license. 36 | * `total` - The total number of units contained in the license key. 37 | * `used` - The number of units assigned to this license key. 38 | 39 | ~> **NOTE:** Labels are not available for unmanaged ESX hosts. 40 | -------------------------------------------------------------------------------- /docs/data-sources/network.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Networking" 3 | page_title: "VMware vSphere: vsphere_network" 4 | sidebar_current: "docs-vsphere-data-source-network" 5 | description: |- 6 | Provides a vSphere network data source. 7 | This can be used to get the general attributes of a vSphere network. 8 | --- 9 | 10 | # vsphere_network 11 | 12 | The `vsphere_network` data source can be used to discover the ID of a network in 13 | vSphere. This can be any network that can be used as the backing for a network 14 | interface for `vsphere_virtual_machine` or any other vSphere resource that 15 | requires a network. This includes standard (host-based) port groups, distributed 16 | port groups, or opaque networks such as those managed by NSX. 17 | 18 | ## Example Usage 19 | 20 | ```hcl 21 | data "vsphere_datacenter" "datacenter" { 22 | name = "dc-01" 23 | } 24 | 25 | data "vsphere_network" "network" { 26 | name = "VM Network" 27 | datacenter_id = data.vsphere_datacenter.datacenter.id 28 | } 29 | ``` 30 | 31 | ## Example Usage 32 | 33 | ```hcl 34 | data "vsphere_datacenter" "datacenter" { 35 | name = "dc-01" 36 | } 37 | 38 | data "vsphere_network" "my_port_group" { 39 | datacenter_id = data.vsphere_datacenter.datacenter.id 40 | name = "VM Network" 41 | filter { 42 | network_type = "Network" 43 | } 44 | } 45 | ``` 46 | 47 | ## Argument Reference 48 | 49 | The following arguments are supported: 50 | 51 | * `name` - (Required) The name of the network. This can be a name or path. 52 | * `datacenter_id` - (Optional) The 53 | [managed object reference ID][docs-about-morefs] of the datacenter the network 54 | is located in. This can be omitted if the search path used in `name` is an 55 | absolute path. For default datacenters, use the `id` attribute from an empty 56 | `vsphere_datacenter` data source. 57 | * `distributed_virtual_switch_uuid` - (Optional) For distributed port group type 58 | network objects, the ID of the distributed virtual switch for which the port 59 | group belongs. It is useful to differentiate port groups with same name using 60 | the distributed virtual switch ID. 61 | * `filter` - (Optional) Apply a filter for the discovered network. 62 | * `network_type`: This is required if you have multiple port groups with the same name. This will be one of `DistributedVirtualPortgroup` for distributed port groups, `Network` for standard (host-based) port groups, or `OpaqueNetwork` for networks managed externally, such as those managed by NSX. 63 | * `retry_timeout` - (Optional) The timeout duration in seconds for the data source to retry read operations. 64 | * `retry_interval` - (Optional) The interval in milliseconds to retry the read operation if `retry_timeout` is set. Default: 500. 65 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 66 | 67 | ## Attribute Reference 68 | 69 | The following attributes are exported: 70 | 71 | * `id`: The [managed object ID][docs-about-morefs] of the network. 72 | * `type`: The managed object type for the discovered network. This will be one 73 | of `DistributedVirtualPortgroup` for distributed port groups, `Network` for 74 | standard (host-based) port groups, or `OpaqueNetwork` for networks managed 75 | externally, such as those managed by NSX. 76 | 77 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 78 | -------------------------------------------------------------------------------- /docs/data-sources/storage_policy.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Storage" 3 | page_title: "VMware vSphere: vsphere_storage_policy" 4 | sidebar_current: "docs-vsphere-data-source-storage-policy" 5 | description: |- 6 | A data source that can be used to get the UUID of a storage policy. 7 | --- 8 | 9 | # vsphere_storage_policy 10 | 11 | The `vsphere_storage_policy` data source can be used to discover the UUID of a 12 | storage policy. This can then be used with other resources or data sources that 13 | use a storage policy. 14 | 15 | ~> **NOTE:** Storage policies are not supported on direct ESXi hosts and 16 | requires vCenter Server. 17 | 18 | ## Example Usage 19 | 20 | ```hcl 21 | data "vsphere_storage_policy" "prod_platinum_replicated" { 22 | name = "prod_platinum_replicated" 23 | } 24 | 25 | data "vsphere_storage_policy" "dev_silver_nonreplicated" { 26 | name = "dev_silver_nonreplicated" 27 | } 28 | ``` 29 | 30 | ## Argument Reference 31 | 32 | The following arguments are supported: 33 | 34 | * `name` - (Required) The name of the storage policy. 35 | 36 | ## Attribute Reference 37 | 38 | The only exported attribute is `id`, which is the UUID of this storage policy. 39 | -------------------------------------------------------------------------------- /docs/data-sources/tag.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_tag" 4 | sidebar_current: "docs-vsphere-data-source-tag-data-source" 5 | description: |- 6 | Provides a vSphere tag data source. 7 | This can be used to reference tags not managed in Terraform. 8 | --- 9 | 10 | # vsphere_tag 11 | 12 | The `vsphere_tag` data source can be used to reference tags that are not managed 13 | by Terraform. Its attributes are exactly the same as the 14 | [`vsphere_tag` resource][resource-tag], and, like importing, the data source 15 | uses a name and category as search criteria. The `id` and other attributes are 16 | populated with the data found by the search. 17 | 18 | [resource-tag]: /docs/providers/vsphere/r/tag.html 19 | 20 | ~> **NOTE:** Tagging is not supported on direct ESXi hosts connections and 21 | requires vCenter Server. 22 | 23 | ## Example Usage 24 | 25 | ```hcl 26 | data "vsphere_tag_category" "category" { 27 | name = "example-category" 28 | } 29 | 30 | data "vsphere_tag" "tag" { 31 | name = "example-tag" 32 | category_id = data.vsphere_tag_category.category.id 33 | } 34 | ``` 35 | 36 | ## Argument Reference 37 | 38 | The following arguments are supported: 39 | 40 | * `name` - (Required) The name of the tag. 41 | * `category_id` - (Required) The ID of the tag category in which the tag is 42 | located. 43 | 44 | ## Attribute Reference 45 | 46 | In addition to the `id` being exported, all of the fields that are available in 47 | the [`vsphere_tag` resource][resource-tag] are also populated. 48 | -------------------------------------------------------------------------------- /docs/data-sources/tag_category.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_tag_category" 4 | sidebar_current: "docs-vsphere-data-source-tag-category" 5 | description: |- 6 | Provides a vSphere tag category data source. 7 | This can be used to reference tag categories not managed in Terraform. 8 | --- 9 | 10 | # vsphere_tag_category 11 | 12 | The `vsphere_tag_category` data source can be used to reference tag categories 13 | that are not managed by Terraform. Its attributes are the same as the 14 | [`vsphere_tag_category` resource][resource-tag-category], and, like importing, 15 | the data source uses a name and category as search criteria. The `id` and other 16 | attributes are populated with the data found by the search. 17 | 18 | [resource-tag-category]: /docs/providers/vsphere/r/tag_category.html 19 | 20 | ~> **NOTE:** Tagging is not supported on direct ESXi hosts connections and 21 | requires vCenter Server. 22 | 23 | ## Example Usage 24 | 25 | ```hcl 26 | data "vsphere_tag_category" "category" { 27 | name = "example-category" 28 | } 29 | ``` 30 | 31 | ## Argument Reference 32 | 33 | The following arguments are supported: 34 | 35 | * `name` - (Required) The name of the tag category. 36 | 37 | ## Attribute Reference 38 | 39 | In addition to the `id` being exported, all of the fields that are available in 40 | the [`vsphere_tag_category` resource][resource-tag-category] are also populated. 41 | -------------------------------------------------------------------------------- /docs/data-sources/vapp_container.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Virtual Machine" 3 | page_title: "VMware vSphere: vsphere_vapp_container" 4 | sidebar_current: "docs-vsphere-data-source-resource-pool" 5 | description: |- 6 | Provides a vSphere vApp container data source. 7 | This can be used to return the general attributes of a vSphere vApp container. 8 | --- 9 | 10 | # vsphere_resource_pool 11 | 12 | The `vsphere_vapp_container` data source can be used to discover the ID of a 13 | vApp container in vSphere. This is useful to return the ID of a vApp container 14 | that you want to use to create virtual machines in using the 15 | [`vsphere_virtual_machine`][docs-virtual-machine-resource] resource. 16 | 17 | [docs-virtual-machine-resource]: /docs/providers/vsphere/r/virtual_machine.html 18 | 19 | ## Example Usage 20 | 21 | ```hcl 22 | data "vsphere_datacenter" "datacenter" { 23 | name = "dc-01" 24 | } 25 | 26 | data "vsphere_vapp_container" "pool" { 27 | name = "vapp-container-01" 28 | datacenter_id = data.vsphere_datacenter.datacenter.id 29 | } 30 | ``` 31 | 32 | ## Argument Reference 33 | 34 | The following arguments are supported: 35 | 36 | * `name` - (Required) The name of the vApp container. This can be a name or 37 | path. 38 | * `datacenter_id` - (Required) The 39 | [managed object reference ID][docs-about-morefs] of the datacenter in which 40 | the vApp container is located. 41 | 42 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 43 | 44 | ## Attribute Reference 45 | 46 | The only exported attribute for this data source is `id`, which represents the 47 | ID of the vApp container that was looked up. 48 | -------------------------------------------------------------------------------- /docs/data-sources/vmfs_disks.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Storage" 3 | page_title: "VMware vSphere: vsphere_vmfs_disks" 4 | sidebar_current: "docs-vsphere-data-source-vmfs-disks" 5 | description: |- 6 | A data source that can be used to discover storage devices that can be used 7 | for VMFS datastores. 8 | --- 9 | 10 | # vsphere_vmfs_disks 11 | 12 | The `vsphere_vmfs_disks` data source can be used to discover the storage 13 | devices available on an ESXi host. This data source can be combined with the 14 | [`vsphere_vmfs_datastore`][data-source-vmfs-datastore] resource to create VMFS 15 | datastores based off a set of discovered disks. 16 | 17 | [data-source-vmfs-datastore]: /docs/providers/vsphere/r/vmfs_datastore.html 18 | 19 | ## Example Usage 20 | 21 | ```hcl 22 | data "vsphere_datacenter" "datacenter" { 23 | name = "dc-01" 24 | } 25 | 26 | data "vsphere_host" "host" { 27 | name = "esxi-01.example.com" 28 | datacenter_id = data.vsphere_datacenter.datacenter.id 29 | } 30 | 31 | data "vsphere_vmfs_disks" "vmfs_disks" { 32 | host_system_id = data.vsphere_host.host.id 33 | rescan = true 34 | filter = "mpx.vmhba1:C0:T[12]:L0" 35 | } 36 | ``` 37 | 38 | ## Argument Reference 39 | 40 | The following arguments are supported: 41 | 42 | * `host_system_id` - (Required) The [managed object ID][docs-about-morefs] of 43 | the host to look for disks on. 44 | 45 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 46 | 47 | * `rescan` - (Optional) Whether or not to rescan storage adapters before 48 | searching for disks. This may lengthen the time it takes to perform the 49 | search. Default: `false`. 50 | * `filter` - (Optional) A regular expression to filter the disks against. Only 51 | disks with canonical names that match will be included. 52 | 53 | ~> **NOTE:** Using a `filter` is recommended if there is any chance the host 54 | will have any specific storage devices added to it that may affect the order of 55 | the output `disks` attribute below, which is lexicographically sorted. 56 | 57 | ## Attribute Reference 58 | 59 | * `disks` - A lexicographically sorted list of devices discovered by the 60 | operation, matching the supplied `filter`, if provided. 61 | -------------------------------------------------------------------------------- /docs/data-sources/vsphere_role.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Security" 3 | page_title: "VMware vSphere: Role" 4 | sidebar_current: "docs-vsphere-data-source-vsphere-role" 5 | description: |- 6 | Provides a vSphere role data source. 7 | --- 8 | 9 | # vsphere_role 10 | 11 | The `vsphere_role` data source can be used to discover the `id` and privileges 12 | associated with a role given its name or display label. 13 | 14 | ## Example Usage 15 | 16 | ```hcl 17 | data "vsphere_role" "terraform_role" { 18 | label = "Terraform to vSphere Integration Role" 19 | } 20 | ``` 21 | 22 | ## Argument Reference 23 | 24 | The following arguments are supported: 25 | 26 | * `label` - (Required) The label of the role. 27 | 28 | ## Attribute Reference 29 | 30 | * `id` - The ID of the role. 31 | * `description` - The description of the role. 32 | * `role_privileges` - The privileges associated with the role. 33 | * `label` - The display label of the role. 34 | -------------------------------------------------------------------------------- /docs/resources/datacenter.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Inventory" 3 | page_title: "VMware vSphere: vsphere_datacenter" 4 | sidebar_current: "docs-vsphere-resource-inventory-datacenter" 5 | description: |- 6 | Provides a VMware vSphere datacenter resource. This can be used as the primary container of inventory objects such as hosts and virtual machines. 7 | --- 8 | 9 | # vsphere_datacenter 10 | 11 | Provides a VMware vSphere datacenter resource. This can be used as the primary 12 | container of inventory objects such as hosts and virtual machines. 13 | 14 | ## Example Usages 15 | 16 | **Create datacenter on the root folder:** 17 | 18 | ```hcl 19 | resource "vsphere_datacenter" "prod_datacenter" { 20 | name = "my_prod_datacenter" 21 | } 22 | ``` 23 | 24 | **Create datacenter on a subfolder:** 25 | 26 | ```hcl 27 | resource "vsphere_datacenter" "research_datacenter" { 28 | name = "my_research_datacenter" 29 | folder = "/research/" 30 | } 31 | ``` 32 | 33 | ## Argument Reference 34 | 35 | The following arguments are supported: 36 | 37 | * `name` - (Required) The name of the datacenter. This name needs to be unique 38 | within the folder. Forces a new resource if changed. 39 | * `folder` - (Optional) The folder where the datacenter should be created. 40 | Forces a new resource if changed. 41 | * `tags` - (Optional) The IDs of any tags to attach to this resource. See 42 | [here][docs-applying-tags] for a reference on how to apply tags. 43 | 44 | [docs-applying-tags]: /docs/providers/vsphere/r/tag.html#using-tags-in-a-supported-resource 45 | 46 | * `custom_attributes` - (Optional) Map of custom attribute ids to value 47 | strings to set for datacenter resource. See 48 | [here][docs-setting-custom-attributes] for a reference on how to set values 49 | for custom attributes. 50 | 51 | [docs-setting-custom-attributes]: /docs/providers/vsphere/r/custom_attribute.html#using-custom-attributes-in-a-supported-resource 52 | 53 | ~> **NOTE:** Custom attributes are unsupported on direct ESXi connections 54 | and require vCenter. 55 | 56 | ## Attribute Reference 57 | 58 | * `id` - The name of this datacenter. This will be changed to the [managed 59 | object ID][docs-about-morefs] in v2.0. 60 | * `moid` - [Managed object ID][docs-about-morefs] of this datacenter. 61 | 62 | [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider 63 | 64 | ## Importing 65 | 66 | An existing datacenter can be [imported][docs-import] into this resource 67 | via supplying the full path to the datacenter. An example is below: 68 | 69 | [docs-import]: /docs/import/index.html 70 | 71 | ```shell 72 | terraform import vsphere_datacenter.dc /dc1 73 | ``` 74 | 75 | The above would import the datacenter named `dc1`. 76 | -------------------------------------------------------------------------------- /docs/resources/guest_os_customization.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Virtual Machine" 3 | page_title: "VMware vSphere: vsphere_guest_os_customization" 4 | sidebar_current: "docs-vsphere-data-guest-os-customization" 5 | description: |- 6 | Provides a VMware vSphere customization specification resource. This can be used to apply a customization specification to the guest operating system of a virtual machine after cloning. 7 | --- 8 | 9 | # vsphere_guest_os_customization 10 | 11 | The `vsphere_guest_os_customization` resource can be used to a customization specification for a guest operating system. 12 | 13 | ~> **NOTE:** The name attribute is unique identifier for the guest OS spec per VC. 14 | 15 | ## Example Usage 16 | 17 | ```hcl 18 | resource "vsphere_guest_os_customization" "windows" { 19 | name = "windows" 20 | type = "Windows" 21 | spec { 22 | windows_options { 23 | run_once_command_list = ["command-1", "command-2"] 24 | computer_name = "windows" 25 | auto_logon = false 26 | auto_logon_count = 0 27 | admin_password = "VMware1!" 28 | time_zone = 004 29 | workgroup = "workgroup" 30 | } 31 | } 32 | } 33 | ``` 34 | 35 | ## Argument Reference 36 | 37 | The following arguments are supported: 38 | 39 | * `name` - (Required) The name of the customization specification is the unique identifier per vCenter Server instance. 40 | * `type` - (Required) The type of customization specification: One among: Windows, Linux. 41 | * `description` - (Optional) The description for the customization specification. 42 | * `spec` - Container object for the Guest OS properties about to be customized . See [virtual machine customizations](virtual_machine#virtual-machine-customizations) 43 | 44 | ## Attribute Reference 45 | 46 | * `last_update_time` - The time of last modification to the customization specification. 47 | * `change_version` - The number of last changed version to the customization specification. 48 | -------------------------------------------------------------------------------- /docs/resources/license.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Administration" 3 | page_title: "VMware vSphere: vsphere_license" 4 | sidebar_current: "docs-vsphere-resource-admin-license" 5 | description: |- 6 | Provides a VMware vSphere license resource. 7 | --- 8 | 9 | # vsphere_license 10 | 11 | Provides a VMware vSphere license resource. This can be used to add and remove license keys. 12 | 13 | ## Example Usage 14 | 15 | ```hcl 16 | resource "vsphere_license" "licenseKey" { 17 | license_key = "00000-00000-00000-00000-00000" 18 | 19 | labels = { 20 | VpxClientLicenseLabel = "example" 21 | } 22 | } 23 | ``` 24 | 25 | ## Argument Reference 26 | 27 | The following arguments are supported: 28 | 29 | * `license_key` - (Required) The license key value. 30 | * `labels` - (Optional) A map of labels to be applied to the license key. 31 | 32 | ~> **NOTE:** Labels are not allowed for unmanaged ESX hosts. 33 | 34 | ## Attributes Reference 35 | 36 | The following attributes are exported: 37 | 38 | * `edition_key` - The product edition of the license key. 39 | * `name` - The display name for the license key. 40 | * `total` - The total number of units contained in the license key. 41 | * `used` - The number of units assigned to this license key. 42 | 43 | -------------------------------------------------------------------------------- /docs/resources/offline_software_depot.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Lifecycle" 3 | page_title: "VMware vSphere: vsphere_offline_software_depot" 4 | sidebar_current: "docs-vsphere-resource-offline-software-depot" 5 | description: |- 6 | Provides a VMware vSphere offline software depot resource.. 7 | --- 8 | 9 | # vsphere_offline_software_depot 10 | 11 | Provides a VMware vSphere offline software depot resource. 12 | 13 | ## Example Usages 14 | 15 | ### Create an Offline Depot 16 | 17 | ```hcl 18 | data "vsphere_offline_software_depot" "depot" { 19 | location = "https://your.company.server/path/to/your/files" 20 | } 21 | ``` 22 | 23 | ## Argument Reference 24 | 25 | * `location` - The URL where the depot source is hosted. 26 | 27 | ## Attribute Reference 28 | 29 | * `component` - The list of custom components in the depot. 30 | * `key` - The identifier of the component. 31 | * `version` - The list of available versions of the component. 32 | * `display_name` - The name of the component. Useful for easier identification. 33 | -------------------------------------------------------------------------------- /docs/resources/virtual_machine_class.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Workload Management" 3 | page_title: "VMware vSphere: vsphere_virtual_machine_class" 4 | sidebar_current: "docs-vsphere-resource-virtual-machine-class" 5 | description: |- 6 | Provides a VMware vSphere virtual machine class resource.. 7 | --- 8 | 9 | # vsphere_virtual_machine_class 10 | 11 | Provides a resource for configuring a Virtual Machine class. 12 | 13 | ## Example Usages 14 | 15 | ### Create a Basic Class 16 | 17 | ```hcl 18 | resource "vsphere_virtual_machine_class" "basic_class" { 19 | name = "basic-class" 20 | cpus = 4 21 | memory = 4096 22 | } 23 | ``` 24 | 25 | ### Create a Class with a vGPU 26 | 27 | ```hcl 28 | resource "vsphere_virtual_machine_class" "vgp_class" { 29 | name = "vgpu-class" 30 | cpus = 4 31 | memory = 4096 32 | memory_reservation = 100 33 | vgpu_devices = ["vgpu1"] 34 | } 35 | ``` 36 | 37 | ## Argument Reference 38 | 39 | * `name` - The name for the class. 40 | * `cpus` - The number of CPUs. 41 | * `memory` - The amount of memory in MB. 42 | * `memory_reservation` - The percentage of memory reservation. 43 | * `vgpu_devices` - The identifiers of the vGPU devices for the class. If this is set memory reservation needs to be 100. 44 | -------------------------------------------------------------------------------- /docs/resources/vsphere_entity_permissions.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Security" 3 | page_title: "VMware vSphere: Entity Permissions" 4 | sidebar_current: "docs-vsphere-entity-permissions" 5 | description: |- 6 | Provides CRUD operations on a vsphere entity permissions. 7 | Permissions can be created on an entity for a given user or group with the 8 | specified roles. 9 | --- 10 | 11 | # vsphere_entity_permissions 12 | 13 | The `vsphere_entity_permissions` resource can be used to create and manage 14 | entity permissions. Permissions can be created on an entity for a given user or 15 | group with the specified role. 16 | 17 | ## Example Usage 18 | 19 | This example creates entity permissions on the virtual machine VM1 for the user 20 | group DCClients with role Datastore consumer and for user group ExternalIDPUsers 21 | with role my_terraform_role. The `entity_id` can be the managed object id (or 22 | uuid for some resources). The `entity_type` is one of the managed object types 23 | which can be found from the managed object types section 24 | [here](https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/). 25 | Keep the permissions sorted alphabetically, ignoring case on `user_or_group` for 26 | a better user experience. 27 | 28 | ```hcl 29 | data "vsphere_datacenter" "datacenter" { 30 | name = "Sample_DC_2" 31 | } 32 | 33 | data "vsphere_virtual_machine" "vm" { 34 | name = "VM1" 35 | datacenter_id = data.vsphere_datacenter.datacenter.id 36 | } 37 | 38 | data "vsphere_role" "role1" { 39 | label = "Datastore consumer (sample)" 40 | } 41 | 42 | resource vsphere_role "role2" { 43 | name = "my_terraform_role" 44 | role_privileges = ["Alarm.Acknowledge", "Alarm.Create", "Datacenter.Move"] 45 | } 46 | 47 | resource "vsphere_entity_permissions" p1 { 48 | entity_id = data.vsphere_virtual_machine.vm.id 49 | entity_type = "VirtualMachine" 50 | permissions { 51 | user_or_group = "vsphere.local\\DCClients" 52 | propagate = true 53 | is_group = true 54 | role_id = data.vsphere_role.role1.id 55 | } 56 | permissions { 57 | user_or_group = "vsphere.local\\ExternalIDPUsers" 58 | propagate = true 59 | is_group = true 60 | role_id = vsphere_role.role2.id 61 | } 62 | } 63 | ``` 64 | 65 | ## Argument Reference 66 | 67 | The following arguments are supported: 68 | 69 | * `entity_id` - (Required) The managed object id (uuid for some entities) on 70 | which permissions are to be created. 71 | * `entity_type` - (Required) The managed object type, types can be found in the 72 | managed object type section 73 | [here](https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/). 74 | * `permissions` - (Required) The permissions to be given on this entity. Keep 75 | the permissions sorted alphabetically on `user_or_group` for a better user 76 | experience. 77 | * `user_or_group` - (Required) The user/group getting the permission. 78 | * `is_group` - (Required) Whether `user_or_group` field refers to a user or a 79 | group. True for a group and false for a user. 80 | * `role_id` - (Required) The role id of the role to be given to the user on 81 | the specified entity. 82 | * `propagate` - (Required) Whether or not this permission propagates down the 83 | hierarchy to sub-entities. 84 | -------------------------------------------------------------------------------- /docs/resources/vsphere_role.md: -------------------------------------------------------------------------------- 1 | --- 2 | subcategory: "Security" 3 | page_title: "VMware vSphere: Role" 4 | sidebar_current: "docs-vsphere-resource-role" 5 | description: |- 6 | Provides CRUD operations on a vsphere role. A role can be created and privileges can be associated with it, 7 | --- 8 | 9 | # vsphere_role 10 | 11 | The `vsphere_role` resource can be used to create and manage roles. Using this resource, privileges can be 12 | associated with the roles. The role can be used while granting permissions to an entity. 13 | 14 | ## Example Usage 15 | 16 | This example creates a role with name my_terraform_role and privileges create, acknowledge for Alarm and 17 | create, move for Datacenter. While providing `role_privileges`, the id of the privilege has to be provided. 18 | The format of the privilege id is privilege name preceded by its categories joined by a `.`. 19 | For example a privilege with path `category->subcategory->privilege` should be provided as 20 | `category.subcategory.privilege`. Keep the `role_privileges` sorted alphabetically for a better user experience. 21 | 22 | ~> **NOTE:** While providing `role_privileges`, the id of the privilege and its categories are to be provided 23 | joined by a `.` . 24 | 25 | ```hcl 26 | resource vsphere_role "role1" { 27 | name = "my_terraform_role" 28 | role_privileges = ["Alarm.Acknowledge", "Alarm.Create", "Datacenter.Create", "Datacenter.Move"] 29 | } 30 | ``` 31 | 32 | ## Argument Reference 33 | 34 | The following arguments are supported: 35 | 36 | * `name` - (Required) The name of the role. 37 | * `role_privileges` - (Optional) The privileges to be associated with this role. 38 | 39 | ## Importing 40 | 41 | An existing role can be imported into this resource by supplying the role id. An example is below: 42 | 43 | ```shell 44 | terraform import vsphere_role.role1 -709298051 45 | ``` 46 | 47 | ~> **NOTE:** System roles can't be imported because they can't be modified or deleted. 48 | Use [`vsphere_role` data source][ref-vsphere-role-data-source] 49 | to read information about system roles. 50 | 51 | [ref-vsphere-role-data-source]: /docs/providers/vsphere/d/vsphere_role.html 52 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/vmware/terraform-provider-vsphere 2 | 3 | go 1.23.8 4 | 5 | require ( 6 | github.com/davecgh/go-spew v1.1.1 7 | github.com/hashicorp/terraform-plugin-log v0.9.0 8 | github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0 9 | github.com/hashicorp/terraform-plugin-testing v1.13.1 10 | github.com/mitchellh/copystructure v1.2.0 11 | github.com/vmware/govmomi v0.51.0 12 | ) 13 | 14 | require ( 15 | github.com/ProtonMail/go-crypto v1.1.6 // indirect 16 | github.com/agext/levenshtein v1.2.2 // indirect 17 | github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect 18 | github.com/cloudflare/circl v1.6.0 // indirect 19 | github.com/fatih/color v1.16.0 // indirect 20 | github.com/golang/protobuf v1.5.4 // indirect 21 | github.com/google/go-cmp v0.7.0 // indirect 22 | github.com/google/uuid v1.6.0 // indirect 23 | github.com/hashicorp/errwrap v1.0.0 // indirect 24 | github.com/hashicorp/go-checkpoint v0.5.0 // indirect 25 | github.com/hashicorp/go-cleanhttp v0.5.2 // indirect 26 | github.com/hashicorp/go-cty v1.5.0 // indirect 27 | github.com/hashicorp/go-hclog v1.6.3 // indirect 28 | github.com/hashicorp/go-multierror v1.1.1 // indirect 29 | github.com/hashicorp/go-plugin v1.6.3 // indirect 30 | github.com/hashicorp/go-retryablehttp v0.7.7 // indirect 31 | github.com/hashicorp/go-uuid v1.0.3 // indirect 32 | github.com/hashicorp/go-version v1.7.0 // indirect 33 | github.com/hashicorp/hc-install v0.9.2 // indirect 34 | github.com/hashicorp/hcl/v2 v2.23.0 // indirect 35 | github.com/hashicorp/logutils v1.0.0 // indirect 36 | github.com/hashicorp/terraform-exec v0.23.0 // indirect 37 | github.com/hashicorp/terraform-json v0.25.0 // indirect 38 | github.com/hashicorp/terraform-plugin-go v0.27.0 // indirect 39 | github.com/hashicorp/terraform-registry-address v0.2.5 // indirect 40 | github.com/hashicorp/terraform-svchost v0.1.1 // indirect 41 | github.com/hashicorp/yamux v0.1.2 // indirect 42 | github.com/mattn/go-colorable v0.1.13 // indirect 43 | github.com/mattn/go-isatty v0.0.20 // indirect 44 | github.com/mitchellh/go-testing-interface v1.14.1 // indirect 45 | github.com/mitchellh/go-wordwrap v1.0.0 // indirect 46 | github.com/mitchellh/mapstructure v1.5.0 // indirect 47 | github.com/mitchellh/reflectwalk v1.0.2 // indirect 48 | github.com/oklog/run v1.0.0 // indirect 49 | github.com/rogpeppe/go-internal v1.8.1 // indirect 50 | github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect 51 | github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect 52 | github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect 53 | github.com/zclconf/go-cty v1.16.2 // indirect 54 | golang.org/x/crypto v0.38.0 // indirect 55 | golang.org/x/mod v0.24.0 // indirect 56 | golang.org/x/net v0.39.0 // indirect 57 | golang.org/x/sync v0.14.0 // indirect 58 | golang.org/x/sys v0.33.0 // indirect 59 | golang.org/x/text v0.25.0 // indirect 60 | golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect 61 | google.golang.org/appengine v1.6.8 // indirect 62 | google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect 63 | google.golang.org/grpc v1.72.1 // indirect 64 | google.golang.org/protobuf v1.36.6 // indirect 65 | ) 66 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package main 6 | 7 | import ( 8 | "flag" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere" 12 | ) 13 | 14 | func main() { 15 | var debugMode bool 16 | flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve") 17 | flag.Parse() 18 | 19 | opts := &plugin.ServeOpts{ 20 | ProviderFunc: vsphere.Provider, 21 | } 22 | 23 | if debugMode { 24 | opts.Debug = true 25 | opts.ProviderAddr = "vmware/vsphere" 26 | } 27 | 28 | plugin.Serve(opts) 29 | } 30 | -------------------------------------------------------------------------------- /scripts/docscheck.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # © Broadcom. All Rights Reserved. 3 | # The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 4 | # SPDX-License-Identifier: MPL-2.0 5 | 6 | docs=$(find ./docs -type f -name "*.md") 7 | error=false 8 | 9 | for doc in $docs; do 10 | dirname=$(dirname "$doc") 11 | category=$(basename "$dirname") 12 | 13 | 14 | case "$category" in 15 | "guides") 16 | # Guides have no requirements. 17 | continue 18 | ;; 19 | 20 | "data-sources") 21 | # Data sources require a subcategory. 22 | grep "^subcategory: " "$doc" > /dev/null 23 | if [[ "$?" == "1" ]]; then 24 | echo "Data source documentation is missing a subcategory: $doc" 25 | error=true 26 | fi 27 | ;; 28 | 29 | "resources") 30 | # Resources require a subcategory. 31 | grep "^subcategory: " "$doc" > /dev/null 32 | if [[ "$?" == "1" ]]; then 33 | echo "Resource documentation is missing a subcategory: $doc" 34 | error=true 35 | fi 36 | ;; 37 | 38 | *) 39 | continue 40 | ;; 41 | esac 42 | done 43 | 44 | if $error; then 45 | exit 1 46 | fi 47 | 48 | echo "==> Done." 49 | exit 0 50 | -------------------------------------------------------------------------------- /scripts/gofmtcheck.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # © Broadcom. All Rights Reserved. 3 | # The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 4 | # SPDX-License-Identifier: MPL-2.0 5 | 6 | 7 | # Check gofmt 8 | echo "==> Checking that code complies with gofmt requirements..." 9 | gofmt_files=$(gofmt -l `find . -name '*.go' | grep -v vendor`) 10 | if [[ -n ${gofmt_files} ]]; then 11 | echo 'gofmt needs running on the following files:' 12 | echo "${gofmt_files}" 13 | echo "You can use the command: \`make fmt\` to reformat code." 14 | exit 1 15 | fi 16 | 17 | exit 0 18 | -------------------------------------------------------------------------------- /terraform-registry-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "metadata": { 4 | "protocol_versions": ["5.0"] 5 | } 6 | } -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | # Acceptance Tests 2 | 3 | This directory contains a provider configuration and shell scripts that can configure a standard testing environment and trigger a test run. 4 | 5 | ## Prerequisites 6 | 7 | You need to provision the bare infrastructure necessary to configure the testing environment. 8 | 9 | This includes: 10 | 11 | * 1 vCenter 12 | * 4 ESX hosts with at least 2 network adapters 13 | * 1 NFS share 14 | 15 | ## Configure Testing Environment 16 | 17 | Create a `.tfvars` file and populate the variables declared in `variables.tf`. 18 | 19 | Run `terraform apply` using the configuration provided in `main.tf` to prepare the environment for testing. 20 | 21 | ## Run Acceptance Tests 22 | 23 | Set the missing values in `setup_env_vars.sh`. 24 | 25 | Execute `run_tests.sh` to run the full test suite or add the `-run` parameter to the `go test` command to run a subset of tests. 26 | -------------------------------------------------------------------------------- /tests/main.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | vsphere = { 4 | source = "hashicorp/vsphere" 5 | } 6 | } 7 | } 8 | 9 | provider "vsphere" { 10 | user = var.vcenter_username 11 | password = var.vcenter_password 12 | vsphere_server = var.vcenter_server 13 | allow_unverified_ssl = true 14 | } 15 | 16 | data "vsphere_host_thumbprint" "thumbprint1" { 17 | address = var.hosts[0].hostname 18 | insecure = true 19 | } 20 | 21 | data "vsphere_host_thumbprint" "thumbprint2" { 22 | address = var.hosts[1].hostname 23 | insecure = true 24 | } 25 | 26 | data "vsphere_host_thumbprint" "thumbprint3" { 27 | address = var.hosts[2].hostname 28 | insecure = true 29 | } 30 | 31 | resource "vsphere_datacenter" "dc" { 32 | name = "acc-test-dc" 33 | } 34 | 35 | resource "vsphere_host" "host1" { 36 | datacenter = vsphere_datacenter.dc.moid 37 | hostname = var.hosts[0].hostname 38 | username = var.hosts[0].username 39 | password = var.hosts[0].password 40 | thumbprint = data.vsphere_host_thumbprint.thumbprint1.id 41 | } 42 | 43 | resource "vsphere_host" "host2" { 44 | datacenter = vsphere_datacenter.dc.moid 45 | hostname = var.hosts[1].hostname 46 | username = var.hosts[1].username 47 | password = var.hosts[1].password 48 | thumbprint = data.vsphere_host_thumbprint.thumbprint2.id 49 | } 50 | 51 | resource "vsphere_host" "host3" { 52 | datacenter = vsphere_datacenter.dc.moid 53 | hostname = var.hosts[2].hostname 54 | username = var.hosts[2].username 55 | password = var.hosts[2].password 56 | thumbprint = data.vsphere_host_thumbprint.thumbprint3.id 57 | } 58 | 59 | resource "vsphere_compute_cluster" "cluster" { 60 | datacenter_id = vsphere_datacenter.dc.moid 61 | name = "acc-test-cluster" 62 | 63 | ha_enabled = true 64 | drs_enabled = true 65 | 66 | host_system_ids = [ 67 | vsphere_host.host1.id, 68 | vsphere_host.host2.id, 69 | ] 70 | } 71 | 72 | resource "vsphere_nas_datastore" "nfs" { 73 | name = "acc-test-nfs" 74 | host_system_ids = [vsphere_host.host1.id, vsphere_host.host2.id] 75 | type = "NFS" 76 | remote_hosts = [var.nfs_host] 77 | remote_path = "/store" 78 | } -------------------------------------------------------------------------------- /tests/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | . setup_env_vars.sh 3 | TF_ACC=1 go test -json -v ../vsphere -timeout 360m 2>&1 | tee gotest.log | gotestfmt -------------------------------------------------------------------------------- /tests/setup_env_vars.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | export TF_ACC=1 3 | export TF_LOG=INFO 4 | export TF_VAR_STORAGE_POLICY="vSAN Default Storage Policy" 5 | export TF_VAR_VSPHERE_CLUSTER=acc-test-cluster 6 | export TF_VAR_VSPHERE_DATACENTER=acc-test-dc 7 | export TF_VAR_VSPHERE_ESXI1= 8 | export TF_VAR_VSPHERE_ESXI1_PASSWORD= 9 | export TF_VAR_VSPHERE_ESXI2= 10 | export TF_VAR_VSPHERE_ESXI2_PASSWORD= 11 | export TF_VAR_VSPHERE_ESXI3= 12 | export TF_VAR_VSPHERE_ESXI3_PASSWORD= 13 | export TF_VAR_VSPHERE_ESXI4= 14 | export TF_VAR_VSPHERE_ESXI4_PASSWORD= 15 | export TF_VAR_VSPHERE_NAS_HOST= 16 | export TF_VAR_VSPHERE_NFS_DS_NAME=acc-test-nfs 17 | export TF_VAR_VSPHERE_PG_NAME="VM Network" 18 | export TF_VAR_VSPHERE_RESOURCE_POOL="New Resource Pool" 19 | export TF_VAR_VSPHERE_DS_VMFS_ESXI1_DISK0="mpx.vmhba0:C0:T1:L0" 20 | export TF_VAR_VSPHERE_SKIP_UNSTABLE_TESTS=true 21 | export VSPHERE_ALLOW_UNVERIFIED_SSL=true 22 | export VSPHERE_PASSWORD= 23 | export VSPHERE_SERVER= 24 | export VSPHERE_USER= 25 | -------------------------------------------------------------------------------- /tests/variables.tf: -------------------------------------------------------------------------------- 1 | variable "vcenter_username" { 2 | description = "Username used to authenticate against the vCenter Server" 3 | type = string 4 | } 5 | 6 | variable "vcenter_password" { 7 | description = "Password used to authenticate against the vCenter Server" 8 | type = string 9 | } 10 | 11 | variable "vcenter_server" { 12 | description = "FQDN or IP Address of the vCenter Server" 13 | type = string 14 | } 15 | 16 | variable "hosts" { 17 | type = list(object({ 18 | hostname = string 19 | password = string 20 | username = string 21 | })) 22 | } 23 | 24 | variable "nfs_host" { 25 | description = "Hostname of the NFS server" 26 | type = string 27 | } -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_compute_cluster.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/clustercomputeresource" 12 | ) 13 | 14 | func dataSourceVSphereComputeCluster() *schema.Resource { 15 | return &schema.Resource{ 16 | Read: dataSourceVSphereComputeClusterRead, 17 | 18 | Schema: map[string]*schema.Schema{ 19 | "name": { 20 | Type: schema.TypeString, 21 | Required: true, 22 | Description: "The name or absolute path to the cluster.", 23 | }, 24 | "datacenter_id": { 25 | Type: schema.TypeString, 26 | Optional: true, 27 | Description: "The managed object ID of the datacenter the cluster is located in. Not required if using an absolute path.", 28 | }, 29 | "resource_pool_id": { 30 | Type: schema.TypeString, 31 | Computed: true, 32 | Description: "The managed object ID of the cluster's root resource pool.", 33 | }, 34 | }, 35 | } 36 | } 37 | 38 | func dataSourceVSphereComputeClusterRead(d *schema.ResourceData, meta interface{}) error { 39 | cluster, err := resourceVSphereComputeClusterGetClusterFromPath(meta, d.Get("name").(string), d.Get("datacenter_id").(string)) 40 | if err != nil { 41 | return fmt.Errorf("error loading cluster: %s", err) 42 | } 43 | props, err := clustercomputeresource.Properties(cluster) 44 | if err != nil { 45 | return fmt.Errorf("error loading cluster properties: %s", err) 46 | } 47 | 48 | d.SetId(cluster.Reference().Value) 49 | _ = d.Set("resource_pool_id", props.ResourcePool.Value) 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_compute_cluster_host_group.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/clustercomputeresource" 12 | ) 13 | 14 | func dataSourceVSphereComputeClusterHostGroup() *schema.Resource { 15 | return &schema.Resource{ 16 | Read: dataSourceVSphereComputeClusterHostGroupRead, 17 | 18 | Schema: map[string]*schema.Schema{ 19 | "name": { 20 | Type: schema.TypeString, 21 | Required: true, 22 | Description: "The unique name of the host group in the cluster.", 23 | }, 24 | "compute_cluster_id": { 25 | Type: schema.TypeString, 26 | Required: true, 27 | Description: "The managed object ID of the cluster.", 28 | }, 29 | "host_system_ids": { 30 | Type: schema.TypeSet, 31 | Computed: true, 32 | Description: "The managed object IDs of the hosts.", 33 | Elem: &schema.Schema{Type: schema.TypeString}, 34 | }, 35 | }, 36 | } 37 | } 38 | 39 | func dataSourceVSphereComputeClusterHostGroupRead(d *schema.ResourceData, meta interface{}) error { 40 | cluster, name, err := resourceVSphereComputeClusterHostGroupObjects(d, meta) 41 | if err != nil { 42 | return fmt.Errorf("cannot locate resource: %s", err) 43 | } 44 | 45 | props, err := clustercomputeresource.Properties(cluster) 46 | if err != nil { 47 | return fmt.Errorf("cannot read cluster properties: %s", err) 48 | } 49 | 50 | hostSystemIDs := make([]string, len(props.Host)) 51 | for i, host := range props.Host { 52 | hostSystemIDs[i] = host.Reference().Value 53 | } 54 | 55 | d.SetId(name) 56 | if err := d.Set("host_system_ids", hostSystemIDs); err != nil { 57 | return fmt.Errorf("cannot set host_system_ids: %s", err) 58 | } 59 | 60 | return nil 61 | } 62 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_compute_cluster_host_group_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 14 | ) 15 | 16 | func TestAccDataSourceVSphereComputeClusterHostGroup_basic(t *testing.T) { 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | }, 22 | Providers: testAccProviders, 23 | Steps: []resource.TestStep{ 24 | { 25 | Config: testAccDataSourceVSphereComputeClusterHostGroupConfig(2), 26 | Check: resource.ComposeTestCheckFunc( 27 | resource.TestCheckResourceAttrPair( 28 | "data.vsphere_compute_cluster_host_group.test", "host_system_ids", 29 | "vsphere_compute_cluster_host_group.cluster_host_group", "host_system_ids", 30 | ), 31 | ), 32 | }, 33 | }, 34 | }) 35 | } 36 | 37 | func testAccDataSourceVSphereComputeClusterHostGroupConfig(count int) string { 38 | return fmt.Sprintf(` 39 | variable hosts { 40 | default = [ %q, %q ] 41 | } 42 | 43 | %s 44 | 45 | data "vsphere_host" "hosts" { 46 | count = %d 47 | name = var.hosts[count.index] 48 | datacenter_id = data.vsphere_datacenter.rootdc1.id 49 | } 50 | 51 | resource "vsphere_compute_cluster_host_group" "cluster_host_group" { 52 | name = "terraform-test-cluster-group" 53 | compute_cluster_id = data.vsphere_compute_cluster.rootcompute_cluster1.id 54 | host_system_ids = data.vsphere_host.hosts.*.id 55 | } 56 | 57 | data "vsphere_compute_cluster_host_group" "test" { 58 | name = "terraform-test-cluster-group" 59 | compute_cluster_id = data.vsphere_compute_cluster.rootcompute_cluster1.id 60 | } 61 | `, 62 | os.Getenv("TF_VAR_VSPHERE_ESXI1"), 63 | os.Getenv("TF_VAR_VSPHERE_ESXI2"), 64 | testhelper.CombineConfigs( 65 | testhelper.ConfigDataRootDC1(), 66 | testhelper.ConfigDataRootPortGroup1(), 67 | testhelper.ConfigDataRootComputeCluster1(), 68 | testhelper.ConfigDataRootDS1(), 69 | testhelper.ConfigDataRootVMNet(), 70 | ), 71 | count) 72 | } 73 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_compute_cluster_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 14 | ) 15 | 16 | func TestAccDataSourceVSphereComputeCluster_basic(t *testing.T) { 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | }, 22 | Providers: testAccProviders, 23 | Steps: []resource.TestStep{ 24 | { 25 | Config: testAccDataSourceVSphereComputeClusterConfigBasic(), 26 | Check: resource.ComposeTestCheckFunc( 27 | resource.TestCheckResourceAttrPair( 28 | "data.vsphere_compute_cluster.compute_cluster_data", "id", 29 | "data.vsphere_compute_cluster.rootcompute_cluster1", "id", 30 | ), 31 | resource.TestCheckResourceAttrPair( 32 | "data.vsphere_compute_cluster.compute_cluster_data", "resource_pool_id", 33 | "data.vsphere_compute_cluster.rootcompute_cluster1", "resource_pool_id", 34 | ), 35 | ), 36 | }, 37 | }, 38 | }) 39 | } 40 | 41 | func TestAccDataSourceVSphereComputeCluster_absolutePathNoDatacenter(t *testing.T) { 42 | resource.Test(t, resource.TestCase{ 43 | PreCheck: func() { 44 | RunSweepers() 45 | testAccPreCheck(t) 46 | }, 47 | Providers: testAccProviders, 48 | Steps: []resource.TestStep{ 49 | { 50 | Config: testAccDataSourceVSphereComputeClusterConfigAbsolutePath(), 51 | Check: resource.ComposeTestCheckFunc( 52 | resource.TestCheckResourceAttrPair( 53 | "data.vsphere_compute_cluster.compute_cluster_data", "id", 54 | "vsphere_compute_cluster.compute_cluster", "id", 55 | ), 56 | resource.TestCheckResourceAttrPair( 57 | "data.vsphere_compute_cluster.compute_cluster_data", "resource_pool_id", 58 | "vsphere_compute_cluster.compute_cluster", "resource_pool_id", 59 | ), 60 | ), 61 | }, 62 | }, 63 | }) 64 | } 65 | 66 | func testAccDataSourceVSphereComputeClusterConfigBasic() string { 67 | return fmt.Sprintf(` 68 | %s 69 | 70 | data "vsphere_compute_cluster" "compute_cluster_data" { 71 | name = "%s" 72 | datacenter_id = data.vsphere_compute_cluster.rootcompute_cluster1.datacenter_id 73 | } 74 | `, 75 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootComputeCluster1()), 76 | os.Getenv("TF_VAR_VSPHERE_CLUSTER"), 77 | ) 78 | } 79 | 80 | func testAccDataSourceVSphereComputeClusterConfigAbsolutePath() string { 81 | return fmt.Sprintf(` 82 | %s 83 | 84 | resource "vsphere_compute_cluster" "compute_cluster" { 85 | name = "testacc-datastore-cluster" 86 | datacenter_id = data.vsphere_datacenter.rootdc1.id 87 | } 88 | 89 | data "vsphere_compute_cluster" "compute_cluster_data" { 90 | name = "/${data.vsphere_datacenter.rootdc1.name}/host/${vsphere_compute_cluster.compute_cluster.name}" 91 | } 92 | `, 93 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootComputeCluster1()), 94 | ) 95 | } 96 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_content_library.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/contentlibrary" 10 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/provider" 11 | ) 12 | 13 | func dataSourceVSphereContentLibrary() *schema.Resource { 14 | return &schema.Resource{ 15 | Read: dataSourceVSphereContentLibraryRead, 16 | Schema: map[string]*schema.Schema{ 17 | "name": { 18 | Type: schema.TypeString, 19 | Required: true, 20 | Description: "The name of the content library.", 21 | }, 22 | }, 23 | } 24 | } 25 | 26 | func dataSourceVSphereContentLibraryRead(d *schema.ResourceData, meta interface{}) error { 27 | c := meta.(*Client).restClient 28 | lib, err := contentlibrary.FromName(c, d.Get("name").(string)) 29 | if err != nil { 30 | return provider.Error(d.Get("name").(string), "dataSourceVSphereContentLibraryRead", err) 31 | } 32 | d.SetId(lib.ID) 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_content_library_item.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/contentlibrary" 10 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/provider" 11 | ) 12 | 13 | func dataSourceVSphereContentLibraryItem() *schema.Resource { 14 | return &schema.Resource{ 15 | Read: dataSourceVSphereContentLibraryItemRead, 16 | Schema: map[string]*schema.Schema{ 17 | "name": { 18 | Type: schema.TypeString, 19 | Required: true, 20 | ForceNew: true, 21 | Description: "The name of the content library item.", 22 | }, 23 | "library_id": { 24 | Type: schema.TypeString, 25 | Required: true, 26 | ForceNew: true, 27 | Description: "ID of the content library to contain item.", 28 | }, 29 | "type": { 30 | Type: schema.TypeString, 31 | Required: true, 32 | ForceNew: true, 33 | Description: "Type of content library item.", 34 | }, 35 | }, 36 | } 37 | } 38 | 39 | func dataSourceVSphereContentLibraryItemRead(d *schema.ResourceData, meta interface{}) error { 40 | rc := meta.(*Client).restClient 41 | lib, _ := contentlibrary.FromID(rc, d.Get("library_id").(string)) 42 | item, err := contentlibrary.ItemFromName(rc, lib, d.Get("name").(string)) 43 | if err != nil { 44 | return provider.Error(d.Get("name").(string), "dataSourceVSphereContentLibraryItemRead", err) 45 | } 46 | _ = d.Set("type", item.Type) 47 | d.SetId(item.ID) 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_content_library_item_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "regexp" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 14 | ) 15 | 16 | func TestAccDataSourceVSphereContentLibraryItem_basic(t *testing.T) { 17 | testAccSkipUnstable(t) 18 | resource.Test(t, resource.TestCase{ 19 | PreCheck: func() { 20 | RunSweepers() 21 | testAccPreCheck(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | Config: testAccDataSourceVSphereContentLibraryItemConfig(), 27 | Check: resource.ComposeTestCheckFunc( 28 | resource.TestMatchResourceAttr( 29 | "data.vsphere_content_library_item.item", "id", regexp.MustCompile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"), 30 | ), 31 | ), 32 | }, 33 | }, 34 | }) 35 | } 36 | 37 | func testAccDataSourceVSphereContentLibraryItemConfig() string { 38 | return fmt.Sprintf(` 39 | %s 40 | 41 | variable "file" { 42 | type = string 43 | default = "%s" 44 | } 45 | 46 | data "vsphere_datastore" "ds" { 47 | datacenter_id = data.vsphere_datacenter.rootdc1.id 48 | name = vsphere_nas_datastore.ds1.name 49 | } 50 | 51 | resource "vsphere_content_library" "library" { 52 | name = "ContentLibrary_test" 53 | storage_backing = [data.vsphere_datastore.ds.id] 54 | description = "Library Description" 55 | } 56 | 57 | resource "vsphere_content_library_item" "item" { 58 | name = "ubuntu" 59 | description = "Ubuntu Description" 60 | library_id = vsphere_content_library.library.id 61 | type = "ovf" 62 | file_url = var.file 63 | } 64 | 65 | data "vsphere_content_library_item" "item" { 66 | name = vsphere_content_library_item.item.name 67 | library_id = vsphere_content_library.library.id 68 | type = "ovf" 69 | } 70 | `, testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootHost1(), testhelper.ConfigDataRootHost2(), testhelper.ConfigResDS1(), testhelper.ConfigDataRootComputeCluster1(), testhelper.ConfigResResourcePool1(), testhelper.ConfigDataRootPortGroup1()), 71 | testhelper.ContentLibraryFiles, 72 | ) 73 | } 74 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_content_library_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "regexp" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 14 | ) 15 | 16 | func TestAccDataSourceVSphereContentLibrary_basic(t *testing.T) { 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | }, 22 | Providers: testAccProviders, 23 | Steps: []resource.TestStep{ 24 | { 25 | Config: testAccDataSourceVSphereContentLibraryConfig(), 26 | Check: resource.ComposeTestCheckFunc( 27 | resource.TestMatchResourceAttr( 28 | "data.vsphere_content_library.library", "id", regexp.MustCompile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"), 29 | ), 30 | ), 31 | }, 32 | }, 33 | }) 34 | } 35 | 36 | func testAccDataSourceVSphereContentLibraryConfig() string { 37 | return fmt.Sprintf(` 38 | %s 39 | 40 | resource "vsphere_content_library" "library" { 41 | name = "ContentLibrary_test" 42 | storage_backing = [ data.vsphere_datastore.rootds1.id ] 43 | description = "Library Description" 44 | } 45 | 46 | data "vsphere_content_library" "library" { 47 | name = vsphere_content_library.library.name 48 | } 49 | 50 | `, 51 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootHost1(), testhelper.ConfigDataRootDS1(), testhelper.ConfigDataRootComputeCluster1(), testhelper.ConfigResResourcePool1(), testhelper.ConfigDataRootPortGroup1()), 52 | ) 53 | } 54 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_custom_attribute.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/govmomi/object" 12 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/customattribute" 13 | ) 14 | 15 | func dataSourceVSphereCustomAttribute() *schema.Resource { 16 | return &schema.Resource{ 17 | Read: dataSourceVSphereCustomAttributeRead, 18 | Schema: map[string]*schema.Schema{ 19 | "name": { 20 | Type: schema.TypeString, 21 | Description: "The display name of the custom attribute.", 22 | Required: true, 23 | }, 24 | "managed_object_type": { 25 | Type: schema.TypeString, 26 | Computed: true, 27 | Description: "Object type for which the custom attribute is valid. If not specified, the attribute is valid for all managed object types.", 28 | }, 29 | }, 30 | } 31 | } 32 | 33 | func dataSourceVSphereCustomAttributeRead(d *schema.ResourceData, meta interface{}) error { 34 | client := meta.(*Client).vimClient 35 | err := customattribute.VerifySupport(client) 36 | if err != nil { 37 | return err 38 | } 39 | 40 | fm, err := object.GetCustomFieldsManager(client.Client) 41 | if err != nil { 42 | return err 43 | } 44 | 45 | field, err := customattribute.ByName(fm, d.Get("name").(string)) 46 | if err != nil { 47 | return err 48 | } 49 | d.SetId(fmt.Sprint(field.Key)) 50 | _ = d.Set("managed_object_type", field.ManagedObjectType) 51 | return nil 52 | } 53 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_custom_attribute_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | ) 13 | 14 | func TestAccDataSourceVSphereCustomAttribute_basic(t *testing.T) { 15 | resource.Test(t, resource.TestCase{ 16 | PreCheck: func() { 17 | RunSweepers() 18 | testAccPreCheck(t) 19 | }, 20 | Providers: testAccProviders, 21 | Steps: []resource.TestStep{ 22 | { 23 | Config: testAccDataSourceVSphereCustomAttributeConfig(), 24 | Check: resource.ComposeTestCheckFunc( 25 | resource.TestCheckResourceAttr( 26 | "data.vsphere_custom_attribute.testacc-attribute-data", 27 | "name", 28 | testAccDataSourceVSphereCustomAttributeConfigName, 29 | ), 30 | resource.TestCheckResourceAttr( 31 | "data.vsphere_custom_attribute.testacc-attribute-data", 32 | "managed_object_type", 33 | testAccDataSourceVSphereCustomAttributeConfigType, 34 | ), 35 | resource.TestCheckResourceAttrPair( 36 | "data.vsphere_custom_attribute.testacc-attribute-data", "id", 37 | "vsphere_custom_attribute.testacc-attribute", "id", 38 | ), 39 | ), 40 | }, 41 | }, 42 | }) 43 | } 44 | 45 | const testAccDataSourceVSphereCustomAttributeConfigName = "testacc-attribute" 46 | const testAccDataSourceVSphereCustomAttributeConfigType = "VirtualMachine" 47 | 48 | func testAccDataSourceVSphereCustomAttributeConfig() string { 49 | return fmt.Sprintf(` 50 | variable "attribute_name" { 51 | default = "%s" 52 | } 53 | 54 | variable "attribute_type" { 55 | default = "%s" 56 | } 57 | 58 | resource "vsphere_custom_attribute" "testacc-attribute" { 59 | name = var.attribute_name 60 | managed_object_type = var.attribute_type 61 | } 62 | 63 | data "vsphere_custom_attribute" "testacc-attribute-data" { 64 | name = vsphere_custom_attribute.testacc-attribute.name 65 | } 66 | `, 67 | testAccDataSourceVSphereCustomAttributeConfigName, 68 | testAccDataSourceVSphereCustomAttributeConfigType, 69 | ) 70 | } 71 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_datacenter.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | "log" 11 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 13 | "github.com/vmware/govmomi/find" 14 | "github.com/vmware/govmomi/view" 15 | "github.com/vmware/govmomi/vim25/mo" 16 | ) 17 | 18 | func dataSourceVSphereDatacenter() *schema.Resource { 19 | return &schema.Resource{ 20 | Read: dataSourceVSphereDatacenterRead, 21 | 22 | Schema: map[string]*schema.Schema{ 23 | "name": { 24 | Type: schema.TypeString, 25 | Description: "The name of the datacenter. This can be a name or path. Can be omitted if there is only one datacenter in your inventory.", 26 | Optional: true, 27 | }, 28 | "virtual_machines": { 29 | Type: schema.TypeSet, 30 | Description: "List of all virtual machines included in the vSphere datacenter object.", 31 | Computed: true, 32 | Elem: &schema.Schema{Type: schema.TypeString}, 33 | }, 34 | }, 35 | } 36 | } 37 | 38 | func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) error { 39 | ctx := context.Background() 40 | client := meta.(*Client).vimClient 41 | datacenter := d.Get("name").(string) 42 | dc, err := getDatacenter(client, datacenter) 43 | if err != nil { 44 | return fmt.Errorf("error fetching datacenter: %s", err) 45 | } 46 | id := dc.Reference().Value 47 | d.SetId(id) 48 | finder := find.NewFinder(client.Client) 49 | finder.SetDatacenter(dc) 50 | viewManager := view.NewManager(client.Client) 51 | view, err := viewManager.CreateContainerView(ctx, dc.Reference(), []string{"VirtualMachine"}, true) 52 | if err != nil { 53 | return fmt.Errorf("error fetching datacenter: %s", err) 54 | } 55 | defer func() { 56 | if err := view.Destroy(ctx); err != nil { 57 | log.Printf("[WARN] Error destroying view during cleanup: %v", err) 58 | } 59 | }() 60 | var vms []mo.VirtualMachine 61 | err = view.Retrieve(ctx, []string{"VirtualMachine"}, []string{"name"}, &vms) 62 | if err != nil { 63 | return fmt.Errorf("error fetching virtual machines: %s", err) 64 | } 65 | var vmNames []string 66 | for v := range vms { 67 | vmNames = append(vmNames, vms[v].Name) 68 | } 69 | err = d.Set("virtual_machines", vmNames) 70 | if err != nil { 71 | return fmt.Errorf("error setting virtual_machines: %s", err) 72 | } 73 | return nil 74 | } 75 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_datacenter_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "regexp" 11 | "testing" 12 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 14 | ) 15 | 16 | var testAccDataSourceVSphereDatacenterExpectedRegexp = regexp.MustCompile("^datacenter-") 17 | 18 | func TestAccDataSourceVSphereDatacenter_basic(t *testing.T) { 19 | resource.Test(t, resource.TestCase{ 20 | PreCheck: func() { 21 | RunSweepers() 22 | testAccPreCheck(t) 23 | }, 24 | Providers: testAccProviders, 25 | Steps: []resource.TestStep{ 26 | { 27 | Config: testAccDataSourceVSphereDatacenterConfig(), 28 | Check: resource.ComposeTestCheckFunc( 29 | resource.TestMatchResourceAttr( 30 | "data.vsphere_datacenter.dc", 31 | "id", 32 | testAccDataSourceVSphereDatacenterExpectedRegexp, 33 | ), 34 | ), 35 | }, 36 | }, 37 | }) 38 | } 39 | 40 | func TestAccDataSourceVSphereDatacenter_defaultDatacenter(t *testing.T) { 41 | resource.Test(t, resource.TestCase{ 42 | PreCheck: func() { 43 | RunSweepers() 44 | testAccPreCheck(t) 45 | }, 46 | Providers: testAccProviders, 47 | Steps: []resource.TestStep{ 48 | { 49 | Config: testAccDataSourceVSphereDatacenterConfigDefault, 50 | Check: resource.ComposeTestCheckFunc( 51 | resource.TestMatchResourceAttr( 52 | "data.vsphere_datacenter.dc", 53 | "id", 54 | testAccDataSourceVSphereDatacenterExpectedRegexp, 55 | ), 56 | ), 57 | }, 58 | }, 59 | }) 60 | } 61 | 62 | func TestAccDataSourceVSphereDatacenter_getVirtualMachines(t *testing.T) { 63 | resource.Test(t, resource.TestCase{ 64 | PreCheck: func() { 65 | RunSweepers() 66 | testAccPreCheck(t) 67 | }, 68 | Providers: testAccProviders, 69 | Steps: []resource.TestStep{ 70 | { 71 | Config: testAccDataSourceVSphereDatacenterConfigGetVirtualMachines(), 72 | Check: resource.ComposeTestCheckFunc( 73 | resource.TestMatchResourceAttr( 74 | "data.vsphere_datacenter.dc", 75 | "id", 76 | testAccDataSourceVSphereDatacenterExpectedRegexp, 77 | ), 78 | testCheckOutputBool("found_virtual_machines", "true"), 79 | ), 80 | }, 81 | }, 82 | }) 83 | } 84 | 85 | func testAccDataSourceVSphereDatacenterConfig() string { 86 | return fmt.Sprintf(` 87 | data "vsphere_datacenter" "dc" { 88 | name = "%s" 89 | } 90 | `, os.Getenv("TF_VAR_VSPHERE_DATACENTER")) 91 | } 92 | 93 | const testAccDataSourceVSphereDatacenterConfigDefault = ` 94 | data "vsphere_datacenter" "dc" {} 95 | ` 96 | 97 | func testAccDataSourceVSphereDatacenterConfigGetVirtualMachines() string { 98 | return fmt.Sprintf(` 99 | data "vsphere_datacenter" "dc" { 100 | name = "%s" 101 | } 102 | output "found_virtual_machines" { 103 | value = length(data.vsphere_datacenter.dc.virtual_machines) >= 1 ? "true" : "false" 104 | } 105 | `, os.Getenv("TF_VAR_VSPHERE_DATACENTER"), 106 | ) 107 | } 108 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_datastore.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/govmomi/object" 12 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/datastore" 13 | ) 14 | 15 | func dataSourceVSphereDatastore() *schema.Resource { 16 | return &schema.Resource{ 17 | Read: dataSourceVSphereDatastoreRead, 18 | 19 | Schema: map[string]*schema.Schema{ 20 | "name": { 21 | Type: schema.TypeString, 22 | Description: "The name or path of the datastore.", 23 | Required: true, 24 | }, 25 | "datacenter_id": { 26 | Type: schema.TypeString, 27 | Description: "The managed object ID of the datacenter the datastore is in. This is not required when using ESXi directly, or if there is only one datacenter in your infrastructure.", 28 | Optional: true, 29 | }, 30 | "stats": { 31 | Type: schema.TypeMap, 32 | Description: "The usage stats of the datastore, include total capacity and free space in bytes.", 33 | Optional: true, 34 | }, 35 | }, 36 | } 37 | } 38 | 39 | func dataSourceVSphereDatastoreRead(d *schema.ResourceData, meta interface{}) error { 40 | client := meta.(*Client).vimClient 41 | 42 | name := d.Get("name").(string) 43 | var dc *object.Datacenter 44 | if dcID, ok := d.GetOk("datacenter_id"); ok { 45 | var err error 46 | dc, err = datacenterFromID(client, dcID.(string)) 47 | if err != nil { 48 | return fmt.Errorf("cannot locate datacenter: %s", err) 49 | } 50 | } 51 | ds, err := datastore.FromPath(client, name, dc) 52 | if err != nil { 53 | return fmt.Errorf("error fetching datastore: %s", err) 54 | } 55 | 56 | d.SetId(ds.Reference().Value) 57 | props, err := datastore.Properties(ds) 58 | if err != nil { 59 | return fmt.Errorf("error getting properties for datastore ID %q: %s", ds.Reference().Value, err) 60 | } 61 | _ = d.Set("stats", map[string]string{"capacity": fmt.Sprintf("%v", props.Summary.Capacity), "free": fmt.Sprintf("%v", props.Summary.FreeSpace)}) 62 | return nil 63 | } 64 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_datastore_cluster.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | 11 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 12 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/datastore" 13 | ) 14 | 15 | func dataSourceVSphereDatastoreCluster() *schema.Resource { 16 | return &schema.Resource{ 17 | Read: dataSourceVSphereDatastoreClusterRead, 18 | 19 | Schema: map[string]*schema.Schema{ 20 | "name": { 21 | Type: schema.TypeString, 22 | Required: true, 23 | Description: "The name or absolute path to the datastore cluster.", 24 | }, 25 | "datacenter_id": { 26 | Type: schema.TypeString, 27 | Optional: true, 28 | Description: "The managed object ID of the datacenter the cluster is located in. Not required if using an absolute path.", 29 | }, 30 | "datastores": { 31 | Type: schema.TypeSet, 32 | Computed: true, 33 | Description: "The names of datastores included in the datastore cluster.", 34 | Elem: &schema.Schema{Type: schema.TypeString}, 35 | }, 36 | }, 37 | } 38 | } 39 | 40 | func dataSourceVSphereDatastoreClusterRead(d *schema.ResourceData, meta interface{}) error { 41 | ctx := context.Background() 42 | client := meta.(*Client).vimClient 43 | pod, err := resourceVSphereDatastoreClusterGetPodFromPath(meta, d.Get("name").(string), d.Get("datacenter_id").(string)) 44 | if err != nil { 45 | return fmt.Errorf("error loading datastore cluster: %s", err) 46 | } 47 | d.SetId(pod.Reference().Value) 48 | var dsNames []string 49 | childDatastores, err := pod.Children(ctx) 50 | if err != nil { 51 | return fmt.Errorf("error retrieving datastores in datastore cluster: %s", err) 52 | } 53 | for d := range childDatastores { 54 | ds, err := datastore.FromID(client, childDatastores[d].Reference().Value) 55 | if err != nil { 56 | return fmt.Errorf("error retrieving datastore: %s", err) 57 | } 58 | dsNames = append(dsNames, ds.Name()) 59 | } 60 | err = d.Set("datastores", dsNames) 61 | if err != nil { 62 | return fmt.Errorf("cannot set datastores: %s", err) 63 | } 64 | return nil 65 | } 66 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_datastore_stats_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 13 | ) 14 | 15 | func TestAccDataSourceVSphereDatastoreStats_basic(t *testing.T) { 16 | resource.Test(t, resource.TestCase{ 17 | PreCheck: func() { 18 | RunSweepers() 19 | testAccPreCheck(t) 20 | }, 21 | Providers: testAccProviders, 22 | Steps: []resource.TestStep{ 23 | { 24 | Config: testAccDataSourceVSphereDatastoreStatsConfig(), 25 | Check: resource.ComposeTestCheckFunc( 26 | testCheckOutputBool("found_free_space", "true"), 27 | testCheckOutputBool("found_capacity", "true"), 28 | testCheckOutputBool("free_values_exist", "true"), 29 | testCheckOutputBool("capacity_values_exist", "true"), 30 | ), 31 | }, 32 | }, 33 | }) 34 | } 35 | 36 | func testAccDataSourceVSphereDatastoreStatsConfig() string { 37 | return fmt.Sprintf(` 38 | %s 39 | 40 | data "vsphere_datastore_stats" "datastore_stats" { 41 | datacenter_id = data.vsphere_datacenter.rootdc1.id 42 | } 43 | 44 | output "found_free_space" { 45 | value = length(data.vsphere_datastore_stats.datastore_stats.free_space) >= 1 ? "true" : "false" 46 | } 47 | 48 | output "found_capacity" { 49 | value = length(data.vsphere_datastore_stats.datastore_stats.capacity) >= 1 ? "true" : "false" 50 | } 51 | 52 | output "free_values_exist" { 53 | value = alltrue([ 54 | for free in values(data.vsphere_datastore_stats.datastore_stats.free_space) : 55 | free >= 1 56 | ]) 57 | } 58 | 59 | output "capacity_values_exist" { 60 | value = alltrue([ 61 | for free in values(data.vsphere_datastore_stats.datastore_stats.capacity) : 62 | free >= 1 63 | ]) 64 | } 65 | `, testhelper.CombineConfigs(testhelper.ConfigDataRootDC1())) 66 | } 67 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_distributed_virtual_switch.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/govmomi/object" 12 | "github.com/vmware/govmomi/vim25/types" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/viapi" 14 | ) 15 | 16 | func dataSourceVSphereDistributedVirtualSwitch() *schema.Resource { 17 | return &schema.Resource{ 18 | Read: dataSourceVSphereDistributedVirtualSwitchRead, 19 | 20 | Schema: map[string]*schema.Schema{ 21 | "name": { 22 | Type: schema.TypeString, 23 | Description: "The name of the distributed virtual switch. This can be a name or path.", 24 | Required: true, 25 | }, 26 | "datacenter_id": { 27 | Type: schema.TypeString, 28 | Description: "The managed object ID of the datacenter the DVS is in. This is required if the supplied path is not an absolute path containing a datacenter and there are multiple datacenters in your infrastructure.", 29 | Optional: true, 30 | }, 31 | "uplinks": { 32 | Type: schema.TypeList, 33 | Computed: true, 34 | Description: "The uplink ports on this DVS.", 35 | Elem: &schema.Schema{Type: schema.TypeString}, 36 | }, 37 | }, 38 | } 39 | } 40 | 41 | func dataSourceVSphereDistributedVirtualSwitchRead(d *schema.ResourceData, meta interface{}) error { 42 | client := meta.(*Client).vimClient 43 | if err := viapi.ValidateVirtualCenter(client); err != nil { 44 | return err 45 | } 46 | 47 | name := d.Get("name").(string) 48 | var dc *object.Datacenter 49 | if dcID, ok := d.GetOk("datacenter_id"); ok { 50 | var err error 51 | dc, err = datacenterFromID(client, dcID.(string)) 52 | if err != nil { 53 | return fmt.Errorf("cannot locate datacenter: %s", err) 54 | } 55 | } 56 | dvs, err := dvsFromPath(client, name, dc) 57 | if err != nil { 58 | return fmt.Errorf("error fetching distributed virtual switch: %s", err) 59 | } 60 | props, err := dvsProperties(dvs) 61 | if err != nil { 62 | return fmt.Errorf("error fetching DVS properties: %s", err) 63 | } 64 | 65 | d.SetId(props.Uuid) 66 | uplinkPolicy := props.Config.(*types.VMwareDVSConfigInfo).UplinkPortPolicy.(*types.DVSNameArrayUplinkPortPolicy) 67 | _ = flattenDVSNameArrayUplinkPortPolicy(d, uplinkPolicy) 68 | 69 | return nil 70 | } 71 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_folder.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/folder" 12 | ) 13 | 14 | func dataSourceVSphereFolder() *schema.Resource { 15 | return &schema.Resource{ 16 | Read: dataSourceVSphereFolderRead, 17 | Schema: map[string]*schema.Schema{ 18 | "path": { 19 | Type: schema.TypeString, 20 | Description: "The absolute path of the folder.", 21 | Required: true, 22 | }, 23 | }, 24 | } 25 | } 26 | 27 | func dataSourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error { 28 | client := meta.(*Client).vimClient 29 | fo, err := folder.FromAbsolutePath(client, d.Get("path").(string)) 30 | if err != nil { 31 | return fmt.Errorf("cannot locate folder: %s", err) 32 | } 33 | 34 | d.SetId(fo.Reference().Value) 35 | 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_folder_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "regexp" 11 | "testing" 12 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 14 | ) 15 | 16 | var testAccDataSourceVSphereFolderExpectedRegexp = regexp.MustCompile("^group-v") 17 | 18 | func TestAccDataSourceVSphereFolder_basic(t *testing.T) { 19 | resource.Test(t, resource.TestCase{ 20 | PreCheck: func() { 21 | RunSweepers() 22 | testAccPreCheck(t) 23 | }, 24 | Providers: testAccProviders, 25 | Steps: []resource.TestStep{ 26 | { 27 | Config: testAccDataSourceVSphereFolderConfig(), 28 | Check: resource.ComposeTestCheckFunc( 29 | resource.TestMatchResourceAttr( 30 | "data.vsphere_folder.folder", 31 | "id", 32 | testAccDataSourceVSphereFolderExpectedRegexp, 33 | ), 34 | ), 35 | }, 36 | }, 37 | }) 38 | } 39 | 40 | func testAccDataSourceVSphereFolderConfig() string { 41 | return fmt.Sprintf(` 42 | data "vsphere_datacenter" "dc" { 43 | name = "%s" 44 | } 45 | 46 | resource "vsphere_folder" "folder" { 47 | path = "test-folder" 48 | type = "vm" 49 | datacenter_id = data.vsphere_datacenter.dc.id 50 | } 51 | 52 | data "vsphere_folder" "folder" { 53 | path = "/${data.vsphere_datacenter.dc.name}/vm/${vsphere_folder.folder.path}" 54 | } 55 | `, os.Getenv("TF_VAR_VSPHERE_DATACENTER")) 56 | } 57 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_guest_os_customization_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/acctest" 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | ) 14 | 15 | func TestAccDataSourceVSphereGOSC_basic(t *testing.T) { 16 | goscName := acctest.RandomWithPrefix("lin") 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | }, 22 | Providers: testAccProviders, 23 | Steps: []resource.TestStep{ 24 | { 25 | Config: testAccDataSourceVSphereGOSCConfig(goscName), 26 | Check: resource.TestCheckResourceAttr("data.vsphere_guest_os_customization.gosc1", "id", goscName), 27 | }, 28 | }, 29 | }) 30 | } 31 | 32 | func testAccDataSourceVSphereGOSCConfig(goscName string) string { 33 | return fmt.Sprintf(` 34 | resource "vsphere_guest_os_customization" "source" { 35 | name = %q 36 | type = "Linux" 37 | spec { 38 | linux_options { 39 | domain = "example.com" 40 | host_name = "linux" 41 | } 42 | } 43 | } 44 | data "vsphere_guest_os_customization" "gosc1" { 45 | name = vsphere_guest_os_customization.source.id 46 | } 47 | `, goscName) 48 | } 49 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_host.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/hostsystem" 12 | ) 13 | 14 | func dataSourceVSphereHost() *schema.Resource { 15 | return &schema.Resource{ 16 | Read: dataSourceVSphereHostRead, 17 | 18 | Schema: map[string]*schema.Schema{ 19 | "name": { 20 | Type: schema.TypeString, 21 | Description: "The name of the host. This can be a name or path. If not provided, the default host is used.", 22 | Optional: true, 23 | }, 24 | "datacenter_id": { 25 | Type: schema.TypeString, 26 | Description: "The managed object ID of the datacenter to look for the host in.", 27 | Required: true, 28 | }, 29 | "resource_pool_id": { 30 | Type: schema.TypeString, 31 | Description: "The managed object ID of the host's root resource pool.", 32 | Computed: true, 33 | }, 34 | }, 35 | } 36 | } 37 | 38 | func dataSourceVSphereHostRead(d *schema.ResourceData, meta interface{}) error { 39 | client := meta.(*Client).vimClient 40 | name := d.Get("name").(string) 41 | dcID := d.Get("datacenter_id").(string) 42 | dc, err := datacenterFromID(client, dcID) 43 | if err != nil { 44 | return fmt.Errorf("error fetching datacenter: %s", err) 45 | } 46 | hs, err := hostsystem.SystemOrDefault(client, name, dc) 47 | if err != nil { 48 | return fmt.Errorf("error fetching host: %s", err) 49 | } 50 | rp, err := hostsystem.ResourcePool(hs) 51 | if err != nil { 52 | return err 53 | } 54 | err = d.Set("resource_pool_id", rp.Reference().Value) 55 | if err != nil { 56 | return err 57 | } 58 | id := hs.Reference().Value 59 | d.SetId(id) 60 | return nil 61 | } 62 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_host_base_images.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | "github.com/vmware/govmomi/vapi/esx/settings/depots" 10 | ) 11 | 12 | func dataSourceVSphereHostBaseImages() *schema.Resource { 13 | return &schema.Resource{ 14 | Read: dataSourceVSphereHostBaseImagesRead, 15 | Schema: map[string]*schema.Schema{ 16 | "version": { 17 | Type: schema.TypeList, 18 | Computed: true, 19 | Description: "The available ESXi versions.", 20 | Elem: &schema.Schema{Type: schema.TypeString}, 21 | }, 22 | }, 23 | } 24 | } 25 | 26 | func dataSourceVSphereHostBaseImagesRead(d *schema.ResourceData, meta interface{}) error { 27 | client := meta.(*Client).restClient 28 | 29 | images, err := depots.NewManager(client).ListBaseImages() 30 | if err != nil { 31 | return err 32 | } 33 | 34 | versions := make([]string, len(images)) 35 | for i, image := range images { 36 | versions[i] = image.Version 37 | } 38 | 39 | d.SetId(versions[0]) 40 | return d.Set("version", versions) 41 | } 42 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_host_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "regexp" 11 | "testing" 12 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 14 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 15 | ) 16 | 17 | func TestAccDataSourceVSphereHost_basic(t *testing.T) { 18 | resource.Test(t, resource.TestCase{ 19 | PreCheck: func() { 20 | RunSweepers() 21 | testAccPreCheck(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | Config: testAccDataSourceVSphereHostConfig(), 27 | Check: resource.ComposeTestCheckFunc( 28 | resource.TestMatchResourceAttr( 29 | "data.vsphere_host.host", 30 | "id", 31 | testAccDataSourceVSphereHostExpectedRegexp(), 32 | ), 33 | ), 34 | }, 35 | }, 36 | }) 37 | } 38 | 39 | func TestAccDataSourceVSphereHost_defaultHost(t *testing.T) { 40 | resource.Test(t, resource.TestCase{ 41 | PreCheck: func() { 42 | RunSweepers() 43 | testAccPreCheck(t) 44 | testAccSkipIfNotEsxi(t) 45 | }, 46 | Providers: testAccProviders, 47 | Steps: []resource.TestStep{ 48 | { 49 | Config: testAccDataSourceVSphereHostConfigDefault(), 50 | Check: resource.ComposeTestCheckFunc( 51 | resource.TestMatchResourceAttr( 52 | "data.vsphere_host.host", 53 | "id", 54 | testAccDataSourceVSphereHostExpectedRegexp(), 55 | ), 56 | ), 57 | }, 58 | }, 59 | }) 60 | } 61 | 62 | func testAccDataSourceVSphereHostExpectedRegexp() *regexp.Regexp { 63 | return regexp.MustCompile("^host-") 64 | } 65 | 66 | func testAccDataSourceVSphereHostConfig() string { 67 | return fmt.Sprintf(` 68 | %s 69 | 70 | data "vsphere_host" "host" { 71 | name = "%s" 72 | datacenter_id = data.vsphere_datacenter.rootdc1.id 73 | } 74 | `, testhelper.ConfigDataRootDC1(), os.Getenv("TF_VAR_VSPHERE_ESXI1")) 75 | } 76 | 77 | func testAccDataSourceVSphereHostConfigDefault() string { 78 | return fmt.Sprintf(` 79 | %s 80 | 81 | data "vsphere_host" "host" { 82 | datacenter_id = data.vsphere_datacenter.rootdc1.id 83 | }`, testhelper.ConfigDataRootDC1()) 84 | } 85 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_host_thumbprint.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "bytes" 9 | // TODO: Transition to crypto/sha256 in next major release. 10 | "crypto/sha1" //nolint 11 | "crypto/tls" 12 | "fmt" 13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 15 | ) 16 | 17 | func dataSourceVSphereHostThumbprint() *schema.Resource { 18 | return &schema.Resource{ 19 | Read: dataSourceVSphereHostThumbprintRead, 20 | Schema: map[string]*schema.Schema{ 21 | "address": { 22 | Type: schema.TypeString, 23 | Required: true, 24 | Description: "The address of the ESXi to extract the thumbprint from.", 25 | }, 26 | "port": { 27 | Type: schema.TypeString, 28 | Optional: true, 29 | Default: "443", 30 | Description: "The port to connect to on the ESXi host.", 31 | }, 32 | "insecure": { 33 | Type: schema.TypeBool, 34 | Optional: true, 35 | Default: false, 36 | Description: "Boolean that can be set to true to disable SSL certificate verification.", 37 | }, 38 | }, 39 | } 40 | } 41 | 42 | func dataSourceVSphereHostThumbprintRead(d *schema.ResourceData, _ interface{}) error { 43 | config := &tls.Config{ 44 | MinVersion: tls.VersionTLS12, // Enforce TLS 1.2 or higher. 45 | } 46 | config.InsecureSkipVerify = d.Get("insecure").(bool) 47 | conn, err := tls.Dial("tcp", d.Get("address").(string)+":"+d.Get("port").(string), config) 48 | if err != nil { 49 | return err 50 | } 51 | cert := conn.ConnectionState().PeerCertificates[0] 52 | fingerprint := sha1.Sum(cert.Raw) 53 | 54 | var buf bytes.Buffer 55 | for i, f := range fingerprint { 56 | if i > 0 { 57 | _, _ = fmt.Fprintf(&buf, ":") 58 | } 59 | _, _ = fmt.Fprintf(&buf, "%02X", f) 60 | } 61 | d.SetId(buf.String()) 62 | return nil 63 | } 64 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_host_thumbprint_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "regexp" 11 | "testing" 12 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 14 | ) 15 | 16 | func TestAccDataSourceVSphereHostThumbprint_basic(t *testing.T) { 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | testAccPreCheck(t) 20 | }, 21 | Providers: testAccProviders, 22 | Steps: []resource.TestStep{ 23 | { 24 | Config: testAccDataSourceVSphereHostThumbprintConfig(), 25 | Check: resource.ComposeTestCheckFunc( 26 | resource.TestMatchResourceAttr("data.vsphere_host_thumbprint.thumb", "id", regexp.MustCompile("([0-9A-F]{2}:)+[0-9A-F]{2}")), 27 | ), 28 | }, 29 | }, 30 | }) 31 | } 32 | 33 | func testAccDataSourceVSphereHostThumbprintConfig() string { 34 | return fmt.Sprintf(` 35 | data "vsphere_host_thumbprint" "thumb" { 36 | address = "%s" 37 | insecure = true 38 | }`, 39 | os.Getenv("TF_VAR_VSPHERE_ESXI4"), 40 | ) 41 | } 42 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_license_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | ) 14 | 15 | func TestAccDataSourceVSphereLicense_basic(t *testing.T) { 16 | testAccSkipUnstable(t) 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | testAccDataSourceVSphereLicensePreCheck(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | Config: testAccDataSourceVSphereLicenseConfig(), 27 | Check: resource.ComposeTestCheckFunc( 28 | resource.TestCheckResourceAttr( 29 | "data.vsphere_license.license", 30 | "id", 31 | os.Getenv("TF_VAR_VSPHERE_LICENSE"), 32 | ), 33 | ), 34 | }, 35 | }, 36 | }) 37 | } 38 | 39 | func testAccDataSourceVSphereLicensePreCheck(t *testing.T) { 40 | if os.Getenv("TF_VAR_VSPHERE_LICENSE") == "" { 41 | t.Skip("set TF_VAR_VSPHERE_LICENSE to run vsphere_license acceptance tests") 42 | } 43 | } 44 | 45 | func testAccDataSourceVSphereLicenseConfig() string { 46 | return fmt.Sprintf(` 47 | data "vsphere_license" "license" { 48 | license_key = "%s" 49 | } 50 | `, os.Getenv("TF_VAR_VSPHERE_LICENSE")) 51 | } 52 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_role.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | "log" 11 | "strconv" 12 | "strings" 13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 15 | "github.com/vmware/govmomi/object" 16 | "github.com/vmware/govmomi/vim25/types" 17 | ) 18 | 19 | func dataSourceVsphereRole() *schema.Resource { 20 | return &schema.Resource{ 21 | Read: dataSourceVSphereRoleRead, 22 | Schema: map[string]*schema.Schema{ 23 | "name": { 24 | Type: schema.TypeString, 25 | Optional: true, 26 | Description: "Name of the role.", 27 | }, 28 | "description": { 29 | Type: schema.TypeString, 30 | Optional: true, 31 | Description: "Description of the role.", 32 | }, 33 | "role_privileges": { 34 | Type: schema.TypeList, 35 | Optional: true, 36 | Description: "Privileges to be associated with the role", 37 | Elem: &schema.Schema{Type: schema.TypeString}, 38 | }, 39 | "label": { 40 | Type: schema.TypeString, 41 | Required: true, 42 | Description: "The display label of the role.", 43 | }, 44 | }, 45 | } 46 | } 47 | 48 | func dataSourceVSphereRoleRead(d *schema.ResourceData, meta interface{}) error { 49 | log.Printf("[DEBUG] : Reading vsphere role with label %s", d.Get("label")) 50 | client := meta.(*Client).vimClient 51 | authorizationManager := object.NewAuthorizationManager(client.Client) 52 | 53 | label := d.Get("label").(string) 54 | roleList, err := authorizationManager.RoleList(context.Background()) 55 | if err != nil { 56 | return fmt.Errorf("error while fetching the role list %s", err) 57 | } 58 | var foundRole = types.AuthorizationRole{} 59 | for _, role := range roleList { 60 | if role.Info != nil && role.Info.GetDescription() != nil { 61 | if label == role.Info.GetDescription().Label { 62 | foundRole = role 63 | } 64 | } 65 | } 66 | 67 | if foundRole.RoleId == 0 { 68 | return fmt.Errorf("role with label %s not found", label) 69 | } 70 | 71 | d.SetId(strconv.Itoa(int(foundRole.RoleId))) 72 | _ = d.Set("name", foundRole.Name) 73 | _ = d.Set("description", foundRole.Info.GetDescription().Summary) 74 | 75 | var arr []string 76 | for _, str := range foundRole.Privilege { 77 | if strings.Split(str, ".")[0] != SystemRole { 78 | arr = append(arr, str) 79 | } 80 | } 81 | _ = d.Set("role_privileges", arr) 82 | return nil 83 | } 84 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_role_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | ) 13 | 14 | const NoAccessRoleDescription = "No access" 15 | const NoAccessRoleName = "NoAccess" 16 | const NoAccessRoleID = "-5" 17 | 18 | func TestAccDataSourceVSphereRole_basic(t *testing.T) { 19 | resource.Test(t, resource.TestCase{ 20 | PreCheck: func() { 21 | testAccPreCheck(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | Config: testAccDataSourceVSphereRoleConfig(), 27 | Check: resource.ComposeTestCheckFunc( 28 | resource.TestCheckResourceAttrPair( 29 | "data.vsphere_role.role1", "id", 30 | "vsphere_role.test-role", "id", 31 | ), 32 | resource.TestCheckResourceAttrPair( 33 | "data.vsphere_role.role1", "name", 34 | "vsphere_role.test-role", "name", 35 | ), 36 | resource.TestCheckResourceAttrPair( 37 | "data.vsphere_role.role1", "role_privileges.0", 38 | "vsphere_role.test-role", "role_privileges.0", 39 | ), 40 | resource.TestCheckResourceAttrPair( 41 | "data.vsphere_role.role1", "role_privileges.1", 42 | "vsphere_role.test-role", "role_privileges.1", 43 | ), 44 | resource.TestCheckResourceAttrPair( 45 | "data.vsphere_role.role1", "role_privileges.2", 46 | "vsphere_role.test-role", "role_privileges.2", 47 | ), 48 | resource.TestCheckResourceAttrPair( 49 | "data.vsphere_role.role1", "role_privileges.3", 50 | "vsphere_role.test-role", "role_privileges.3", 51 | ), 52 | ), 53 | }, 54 | }, 55 | }) 56 | } 57 | 58 | func TestAccDataSourceVSphereRole_systemRoleData(t *testing.T) { 59 | resource.Test(t, resource.TestCase{ 60 | PreCheck: func() { 61 | testAccPreCheck(t) 62 | }, 63 | Providers: testAccProviders, 64 | Steps: []resource.TestStep{ 65 | { 66 | Config: testAccDataSourceVSphereRoleSystemRoleConfig(), 67 | Check: resource.ComposeTestCheckFunc( 68 | resource.TestCheckResourceAttr("data.vsphere_role.role1", "name", NoAccessRoleName), 69 | resource.TestCheckResourceAttr("data.vsphere_role.role1", "id", NoAccessRoleID), 70 | resource.TestCheckResourceAttr("data.vsphere_role.role1", "role_privileges.#", "0")), 71 | }, 72 | }, 73 | }) 74 | } 75 | 76 | func testAccDataSourceVSphereRoleConfig() string { 77 | return fmt.Sprintf(` 78 | resource "vsphere_role" test-role { 79 | name = "terraform-test-role1" 80 | role_privileges = ["%s", "%s", "%s", "%s"] 81 | } 82 | 83 | data "vsphere_role" "role1" { 84 | label = vsphere_role.test-role.label 85 | } 86 | `, Privilege1, 87 | Privilege2, 88 | Privilege3, 89 | Privilege4, 90 | ) 91 | } 92 | 93 | func testAccDataSourceVSphereRoleSystemRoleConfig() string { 94 | return fmt.Sprintf(` 95 | data "vsphere_role" "role1" { 96 | label = "%s" 97 | } 98 | `, 99 | NoAccessRoleDescription, 100 | ) 101 | } 102 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_storage_policy.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/spbm" 10 | ) 11 | 12 | func dataSourceVSphereStoragePolicy() *schema.Resource { 13 | return &schema.Resource{ 14 | Read: dataSourceVSphereStoragePolicyRead, 15 | Schema: map[string]*schema.Schema{ 16 | "name": { 17 | Type: schema.TypeString, 18 | Description: "The display name of the storage policy.", 19 | Required: true, 20 | }, 21 | }, 22 | } 23 | } 24 | 25 | func dataSourceVSphereStoragePolicyRead(d *schema.ResourceData, meta interface{}) error { 26 | client := meta.(*Client).vimClient 27 | 28 | id, err := spbm.PolicyIDByName(client, d.Get("name").(string)) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | d.SetId(id) 34 | return nil 35 | } 36 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_storage_policy_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | // Storage policy resource is needed. 8 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_tag.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 8 | 9 | func dataSourceVSphereTag() *schema.Resource { 10 | return &schema.Resource{ 11 | Read: dataSourceVSphereTagRead, 12 | Schema: map[string]*schema.Schema{ 13 | "name": { 14 | Type: schema.TypeString, 15 | Description: "The display name of the tag.", 16 | Required: true, 17 | }, 18 | "category_id": { 19 | Type: schema.TypeString, 20 | Description: "The unique identifier of the parent category for this tag.", 21 | Required: true, 22 | }, 23 | "description": { 24 | Type: schema.TypeString, 25 | Description: "The description of the tag.", 26 | Computed: true, 27 | }, 28 | }, 29 | } 30 | } 31 | 32 | func dataSourceVSphereTagRead(d *schema.ResourceData, meta interface{}) error { 33 | tm, err := meta.(*Client).TagsManager() 34 | if err != nil { 35 | return err 36 | } 37 | 38 | name := d.Get("name").(string) 39 | categoryID := d.Get("category_id").(string) 40 | 41 | tagID, err := tagByName(tm, name, categoryID) 42 | if err != nil { 43 | return err 44 | } 45 | 46 | d.SetId(tagID) 47 | return resourceVSphereTagRead(d, meta) 48 | } 49 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_tag_category.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 8 | 9 | func dataSourceVSphereTagCategory() *schema.Resource { 10 | return &schema.Resource{ 11 | Read: dataSourceVSphereTagCategoryRead, 12 | Schema: map[string]*schema.Schema{ 13 | "name": { 14 | Type: schema.TypeString, 15 | Description: "The display name of the category.", 16 | Required: true, 17 | }, 18 | "description": { 19 | Type: schema.TypeString, 20 | Computed: true, 21 | Description: "The description of the category.", 22 | }, 23 | "cardinality": { 24 | Type: schema.TypeString, 25 | Computed: true, 26 | Description: "The associated cardinality of the category. Can be one of SINGLE (object can only be assigned one tag in this category) or MULTIPLE (object can be assigned multiple tags in this category).", 27 | }, 28 | "associable_types": { 29 | Type: schema.TypeSet, 30 | Computed: true, 31 | Description: "Object types to which this category's tags can be attached.", 32 | Elem: &schema.Schema{Type: schema.TypeString}, 33 | }, 34 | }, 35 | } 36 | } 37 | 38 | func dataSourceVSphereTagCategoryRead(d *schema.ResourceData, meta interface{}) error { 39 | tm, err := meta.(*Client).TagsManager() 40 | if err != nil { 41 | return err 42 | } 43 | 44 | id, err := tagCategoryByName(tm, d.Get("name").(string)) 45 | if err != nil { 46 | return err 47 | } 48 | 49 | d.SetId(id) 50 | return resourceVSphereTagCategoryRead(d, meta) 51 | } 52 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_tag_category_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | ) 13 | 14 | func TestAccDataSourceVSphereTagCategory_basic(t *testing.T) { 15 | resource.Test(t, resource.TestCase{ 16 | PreCheck: func() { 17 | RunSweepers() 18 | testAccPreCheck(t) 19 | }, 20 | Providers: testAccProviders, 21 | Steps: []resource.TestStep{ 22 | { 23 | Config: testAccDataSourceVSphereTagCategoryConfig(), 24 | Check: resource.ComposeTestCheckFunc( 25 | resource.TestCheckResourceAttr( 26 | "data.vsphere_tag_category.testacc-category-data", 27 | "name", 28 | testAccDataSourceVSphereTagCategoryConfigName, 29 | ), 30 | resource.TestCheckResourceAttr( 31 | "data.vsphere_tag_category.testacc-category-data", 32 | "description", 33 | testAccDataSourceVSphereTagCategoryConfigDescription, 34 | ), 35 | resource.TestCheckResourceAttr( 36 | "data.vsphere_tag_category.testacc-category-data", 37 | "cardinality", 38 | testAccDataSourceVSphereTagCategoryConfigCardinality, 39 | ), 40 | resource.TestCheckResourceAttr( 41 | "data.vsphere_tag_category.testacc-category-data", 42 | "associable_types.#", 43 | "1", 44 | ), 45 | resource.TestCheckTypeSetElemAttr( 46 | "data.vsphere_tag_category.testacc-category-data", 47 | "associable_types.*", 48 | testAccDataSourceVSphereTagCategoryConfigAssociableType, 49 | ), 50 | resource.TestCheckResourceAttrPair( 51 | "data.vsphere_tag_category.testacc-category-data", "id", 52 | "vsphere_tag_category.testacc-category", "id", 53 | ), 54 | ), 55 | }, 56 | }, 57 | }) 58 | } 59 | 60 | const testAccDataSourceVSphereTagCategoryConfigName = "testacc-category" 61 | const testAccDataSourceVSphereTagCategoryConfigDescription = "Managed by Terraform" 62 | const testAccDataSourceVSphereTagCategoryConfigCardinality = vSphereTagCategoryCardinalitySingle 63 | const testAccDataSourceVSphereTagCategoryConfigAssociableType = vSphereTagTypeVirtualMachine 64 | 65 | func testAccDataSourceVSphereTagCategoryConfig() string { 66 | return fmt.Sprintf(` 67 | variable "tag_category_name" { 68 | default = "%s" 69 | } 70 | 71 | variable "tag_category_description" { 72 | default = "%s" 73 | } 74 | 75 | variable "tag_category_cardinality" { 76 | default = "%s" 77 | } 78 | 79 | variable "tag_category_associable_types" { 80 | default = [ 81 | "%s", 82 | ] 83 | } 84 | 85 | resource "vsphere_tag_category" "testacc-category" { 86 | name = var.tag_category_name 87 | description = var.tag_category_description 88 | cardinality = var.tag_category_cardinality 89 | 90 | associable_types = var.tag_category_associable_types 91 | } 92 | 93 | data "vsphere_tag_category" "testacc-category-data" { 94 | name = vsphere_tag_category.testacc-category.name 95 | } 96 | `, 97 | testAccDataSourceVSphereTagCategoryConfigName, 98 | testAccDataSourceVSphereTagCategoryConfigDescription, 99 | testAccDataSourceVSphereTagCategoryConfigCardinality, 100 | testAccDataSourceVSphereTagCategoryConfigAssociableType, 101 | ) 102 | } 103 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_tag_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | ) 13 | 14 | func TestAccDataSourceVSphereTag_basic(t *testing.T) { 15 | resource.Test(t, resource.TestCase{ 16 | PreCheck: func() { 17 | RunSweepers() 18 | testAccPreCheck(t) 19 | }, 20 | Providers: testAccProviders, 21 | Steps: []resource.TestStep{ 22 | { 23 | Config: testAccDataSourceVSphereTagConfig(), 24 | Check: resource.ComposeTestCheckFunc( 25 | resource.TestCheckResourceAttr( 26 | "data.vsphere_tag.testacc-tag-data", 27 | "name", 28 | testAccDataSourceVSphereTagConfigName, 29 | ), 30 | resource.TestCheckResourceAttr( 31 | "data.vsphere_tag.testacc-tag-data", 32 | "description", 33 | testAccDataSourceVSphereTagConfigDescription, 34 | ), 35 | resource.TestCheckResourceAttrPair( 36 | "data.vsphere_tag.testacc-tag-data", "id", 37 | "vsphere_tag.testacc-tag", "id", 38 | ), 39 | resource.TestCheckResourceAttrPair( 40 | "data.vsphere_tag.testacc-tag-data", "category_id", 41 | "vsphere_tag_category.testacc-category", "id", 42 | ), 43 | ), 44 | }, 45 | }, 46 | }) 47 | } 48 | 49 | const testAccDataSourceVSphereTagConfigName = "testacc-tag" 50 | const testAccDataSourceVSphereTagConfigDescription = "Managed by Terraform" 51 | 52 | func testAccDataSourceVSphereTagConfig() string { 53 | return fmt.Sprintf(` 54 | variable "tag_category_name" { 55 | default = "%s" 56 | } 57 | 58 | variable "tag_category_description" { 59 | default = "%s" 60 | } 61 | 62 | variable "tag_category_cardinality" { 63 | default = "%s" 64 | } 65 | 66 | variable "tag_category_associable_types" { 67 | default = [ 68 | "%s", 69 | ] 70 | } 71 | 72 | variable "tag_name" { 73 | default = "%s" 74 | } 75 | 76 | variable "tag_description" { 77 | default = "%s" 78 | } 79 | 80 | resource "vsphere_tag_category" "testacc-category" { 81 | name = var.tag_category_name 82 | description = var.tag_category_description 83 | cardinality = var.tag_category_cardinality 84 | 85 | associable_types = var.tag_category_associable_types 86 | } 87 | 88 | resource "vsphere_tag" "testacc-tag" { 89 | name = var.tag_name 90 | description = var.tag_description 91 | category_id = vsphere_tag_category.testacc-category.id 92 | } 93 | 94 | data "vsphere_tag" "testacc-tag-data" { 95 | name = vsphere_tag.testacc-tag.name 96 | category_id = vsphere_tag.testacc-tag.category_id 97 | } 98 | `, 99 | testAccDataSourceVSphereTagCategoryConfigName, 100 | testAccDataSourceVSphereTagCategoryConfigDescription, 101 | testAccDataSourceVSphereTagCategoryConfigCardinality, 102 | testAccDataSourceVSphereTagCategoryConfigAssociableType, 103 | testAccDataSourceVSphereTagConfigName, 104 | testAccDataSourceVSphereTagConfigDescription, 105 | ) 106 | } 107 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_vapp_container.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/vappcontainer" 12 | ) 13 | 14 | func dataSourceVSphereVAppContainer() *schema.Resource { 15 | return &schema.Resource{ 16 | Read: dataSourceVSphereVAppContainerRead, 17 | Schema: map[string]*schema.Schema{ 18 | "name": { 19 | Type: schema.TypeString, 20 | Required: true, 21 | Description: "The name of the vApp container.", 22 | }, 23 | "datacenter_id": { 24 | Type: schema.TypeString, 25 | Required: true, 26 | Description: "The Managed Object ID of the datacenter.", 27 | }, 28 | }, 29 | } 30 | } 31 | 32 | func dataSourceVSphereVAppContainerRead(d *schema.ResourceData, meta interface{}) error { 33 | client, err := resourceVSphereVAppContainerClient(meta) 34 | if err != nil { 35 | return err 36 | } 37 | dc, err := datacenterFromID(client, d.Get("datacenter_id").(string)) 38 | if err != nil { 39 | return fmt.Errorf("cannot locate datacenter: %s", err) 40 | } 41 | vc, err := vappcontainer.FromPath(client, d.Get("name").(string), dc) 42 | if err != nil { 43 | return fmt.Errorf("cannot locate vApp Container: %s", err) 44 | } 45 | d.SetId(vc.Reference().Value) 46 | return nil 47 | } 48 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_vapp_container_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "regexp" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 14 | ) 15 | 16 | func TestAccDataSourceVSphereVAppContainer_basic(t *testing.T) { 17 | resource.Test(t, resource.TestCase{ 18 | PreCheck: func() { 19 | RunSweepers() 20 | testAccPreCheck(t) 21 | testAccSkipIfEsxi(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | ExpectNonEmptyPlan: true, 27 | Config: testAccDataSourceVSphereVAppContainerConfig(), 28 | Check: resource.ComposeTestCheckFunc( 29 | resource.TestMatchResourceAttr("data.vsphere_vapp_container.container", "id", regexp.MustCompile("^resgroup-")), 30 | ), 31 | }, 32 | }, 33 | }) 34 | } 35 | 36 | func TestAccDataSourceVSphereVAppContainer_path(t *testing.T) { 37 | resource.Test(t, resource.TestCase{ 38 | PreCheck: func() { 39 | RunSweepers() 40 | testAccPreCheck(t) 41 | testAccSkipIfEsxi(t) 42 | }, 43 | Providers: testAccProviders, 44 | Steps: []resource.TestStep{ 45 | { 46 | ExpectNonEmptyPlan: true, 47 | Config: testAccDataSourceVSphereVAppContainerPathConfig(), 48 | Check: resource.ComposeTestCheckFunc( 49 | resource.TestMatchResourceAttr("data.vsphere_vapp_container.container", "id", regexp.MustCompile("^resgroup-")), 50 | ), 51 | }, 52 | }, 53 | }) 54 | } 55 | 56 | func testAccDataSourceVSphereVAppContainerConfig() string { 57 | return fmt.Sprintf(` 58 | %s 59 | 60 | data "vsphere_datacenter" "dc" { 61 | name = data.vsphere_datacenter.rootdc1.name 62 | } 63 | 64 | resource "vsphere_vapp_container" "vapp" { 65 | name = "vapp-test" 66 | parent_resource_pool_id = data.vsphere_compute_cluster.rootcompute_cluster1.resource_pool_id 67 | } 68 | 69 | data "vsphere_vapp_container" "container" { 70 | name = vsphere_vapp_container.vapp.name 71 | datacenter_id = data.vsphere_datacenter.rootdc1.id 72 | } 73 | `, 74 | 75 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1(), testhelper.ConfigDataRootComputeCluster1()), 76 | ) 77 | } 78 | 79 | func testAccDataSourceVSphereVAppContainerPathConfig() string { 80 | return fmt.Sprintf(` 81 | %s 82 | 83 | data "vsphere_datacenter" "dc" { 84 | name = data.vsphere_datacenter.rootdc1.name 85 | } 86 | 87 | resource "vsphere_vapp_container" "vapp" { 88 | name = "vapp-test" 89 | parent_resource_pool_id = data.vsphere_compute_cluster.rootcompute_cluster1.resource_pool_id 90 | } 91 | 92 | data "vsphere_vapp_container" "container" { 93 | name = "/${data.vsphere_datacenter.rootdc1.name}/vm/${vsphere_vapp_container.vapp.name}" 94 | datacenter_id = data.vsphere_datacenter.rootdc1.id 95 | } 96 | `, 97 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1(), testhelper.ConfigDataRootComputeCluster1()), 98 | ) 99 | } 100 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_vmfs_disks.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | "regexp" 11 | "sort" 12 | "time" 13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 15 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" 16 | "github.com/vmware/govmomi/vim25/mo" 17 | "github.com/vmware/govmomi/vim25/types" 18 | ) 19 | 20 | func dataSourceVSphereVmfsDisks() *schema.Resource { 21 | return &schema.Resource{ 22 | Read: dataSourceVSphereVmfsDisksRead, 23 | 24 | Schema: map[string]*schema.Schema{ 25 | "host_system_id": { 26 | Type: schema.TypeString, 27 | Description: "The managed object ID of the host to search for disks on.", 28 | Required: true, 29 | }, 30 | "rescan": { 31 | Type: schema.TypeBool, 32 | Description: "Rescan the system for disks before querying. This may lengthen the time it takes to gather information.", 33 | Optional: true, 34 | }, 35 | "filter": { 36 | Type: schema.TypeString, 37 | Description: "A regular expression to filter the disks against. Only disks with canonical names that match will be included.", 38 | Optional: true, 39 | ValidateFunc: validation.StringIsValidRegExp, 40 | }, 41 | "disks": { 42 | Type: schema.TypeList, 43 | Description: "The names of the disks discovered by the search.", 44 | Computed: true, 45 | Elem: &schema.Schema{Type: schema.TypeString}, 46 | }, 47 | }, 48 | } 49 | } 50 | 51 | func dataSourceVSphereVmfsDisksRead(d *schema.ResourceData, meta interface{}) error { 52 | client := meta.(*Client).vimClient 53 | hsID := d.Get("host_system_id").(string) 54 | ss, err := hostStorageSystemFromHostSystemID(client, hsID) 55 | if err != nil { 56 | return fmt.Errorf("error loading host storage system: %s", err) 57 | } 58 | 59 | if d.Get("rescan").(bool) { 60 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 61 | defer cancel() 62 | if err := ss.RescanAllHba(ctx); err != nil { 63 | return err 64 | } 65 | } 66 | 67 | var hss mo.HostStorageSystem 68 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 69 | defer cancel() 70 | if err := ss.Properties(ctx, ss.Reference(), nil, &hss); err != nil { 71 | return fmt.Errorf("error querying storage system properties: %s", err) 72 | } 73 | 74 | d.SetId(time.Now().UTC().String()) 75 | 76 | var disks []string 77 | for _, sl := range hss.StorageDeviceInfo.ScsiLun { 78 | if hsd, ok := sl.(*types.HostScsiDisk); ok { 79 | if matched, _ := regexp.MatchString(d.Get("filter").(string), hsd.CanonicalName); matched { 80 | disks = append(disks, hsd.CanonicalName) 81 | } 82 | } 83 | } 84 | 85 | sort.Strings(disks) 86 | 87 | if err := d.Set("disks", disks); err != nil { 88 | return fmt.Errorf("error saving results to state: %s", err) 89 | } 90 | 91 | return nil 92 | } 93 | -------------------------------------------------------------------------------- /vsphere/data_source_vsphere_vmfs_disks_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/hashicorp/terraform-plugin-testing/terraform" 14 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 15 | ) 16 | 17 | func TestAccDataSourceVSphereVmfsDisks_basic(t *testing.T) { 18 | resource.Test(t, resource.TestCase{ 19 | PreCheck: func() { 20 | RunSweepers() 21 | testAccPreCheck(t) 22 | }, 23 | Providers: testAccProviders, 24 | Steps: []resource.TestStep{ 25 | { 26 | Config: testAccDataSourceVSphereVmfsDisksConfig(), 27 | Check: resource.ComposeTestCheckFunc( 28 | testCheckOutputBool("found", "true"), 29 | ), 30 | }, 31 | }, 32 | }) 33 | } 34 | 35 | // testCheckOutputBool checks an output in the Terraform configuration 36 | func testCheckOutputBool(name string, value string) resource.TestCheckFunc { 37 | return func(s *terraform.State) error { 38 | ms := s.RootModule() 39 | rs, ok := ms.Outputs[name] 40 | if !ok { 41 | return fmt.Errorf("not found: %s", name) 42 | } 43 | 44 | if rs.Value.(string) != value { 45 | return fmt.Errorf( 46 | "output '%s': expected %#v, got %#v", 47 | name, 48 | value, 49 | rs) 50 | } 51 | 52 | return nil 53 | } 54 | } 55 | 56 | func testAccDataSourceVSphereVmfsDisksConfig() string { 57 | return fmt.Sprintf(` 58 | %s 59 | 60 | data "vsphere_host" "esxi_host" { 61 | name = "%s" 62 | datacenter_id = data.vsphere_datacenter.rootdc1.id 63 | } 64 | 65 | data "vsphere_vmfs_disks" "available" { 66 | host_system_id = data.vsphere_host.esxi_host.id 67 | rescan = true 68 | } 69 | 70 | output "found" { 71 | value = length(data.vsphere_vmfs_disks.available.disks) >= 1 ? "true" : "false" 72 | } 73 | `, 74 | testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), 75 | os.Getenv("TF_VAR_VSPHERE_ESXI3"), 76 | ) 77 | } 78 | -------------------------------------------------------------------------------- /vsphere/datacenter_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | 11 | "github.com/vmware/govmomi" 12 | "github.com/vmware/govmomi/find" 13 | "github.com/vmware/govmomi/object" 14 | "github.com/vmware/govmomi/vim25/mo" 15 | "github.com/vmware/govmomi/vim25/types" 16 | ) 17 | 18 | // getDatacenter gets the higher-level datacenter object for the datacenter 19 | // name supplied by dc. 20 | // 21 | // The default datacenter is denoted by using an empty string. When working 22 | // with ESXi directly, the default datacenter is always selected. 23 | func getDatacenter(c *govmomi.Client, dc string) (*object.Datacenter, error) { 24 | finder := find.NewFinder(c.Client, true) 25 | t := c.ServiceContent.About.ApiType 26 | switch t { 27 | case "HostAgent": 28 | return finder.DefaultDatacenter(context.TODO()) 29 | case "VirtualCenter": 30 | if dc != "" { 31 | return finder.Datacenter(context.TODO(), dc) 32 | } 33 | return finder.DefaultDatacenter(context.TODO()) 34 | } 35 | return nil, fmt.Errorf("unsupported ApiType: %s", t) 36 | } 37 | 38 | // datacenterFromID locates a Datacenter by its managed object reference ID. 39 | func datacenterFromID(client *govmomi.Client, id string) (*object.Datacenter, error) { 40 | finder := find.NewFinder(client.Client, false) 41 | 42 | ref := types.ManagedObjectReference{ 43 | Type: "Datacenter", 44 | Value: id, 45 | } 46 | 47 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 48 | defer cancel() 49 | ds, err := finder.ObjectReference(ctx, ref) 50 | if err != nil { 51 | return nil, fmt.Errorf("could not find datacenter with id: %s: %s", id, err) 52 | } 53 | return ds.(*object.Datacenter), nil 54 | } 55 | 56 | func datacenterCustomAttributes(dc *object.Datacenter) (*mo.Datacenter, error) { 57 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 58 | defer cancel() 59 | var props mo.Datacenter 60 | if err := dc.Properties(ctx, dc.Reference(), []string{"customValue"}, &props); err != nil { 61 | return nil, err 62 | } 63 | return &props, nil 64 | } 65 | 66 | func listDatacenters(client *govmomi.Client) ([]*object.Datacenter, error) { 67 | return dcsByPath(client, "/") 68 | } 69 | 70 | func dcsByPath(client *govmomi.Client, path string) ([]*object.Datacenter, error) { 71 | ctx := context.TODO() 72 | var dcs []*object.Datacenter 73 | finder := find.NewFinder(client.Client, false) 74 | if path != "/" { 75 | path += "/*" 76 | } 77 | es, err := finder.ManagedObjectListChildren(ctx, path, "datacenter", "folder") 78 | if err != nil { 79 | return nil, err 80 | } 81 | for _, id := range es { 82 | switch { 83 | case id.Object.Reference().Type == "Datacenter": 84 | dc, err := datacenterFromID(client, id.Object.Reference().Value) 85 | if err != nil { 86 | return nil, err 87 | } 88 | dcs = append(dcs, dc) 89 | case id.Object.Reference().Type == "Folder": 90 | newDCs, err := dcsByPath(client, id.Path) 91 | if err != nil { 92 | return nil, err 93 | } 94 | dcs = append(dcs, newDCs...) 95 | default: 96 | continue 97 | } 98 | } 99 | return dcs, nil 100 | } 101 | -------------------------------------------------------------------------------- /vsphere/host_network_system_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | 11 | "github.com/vmware/govmomi" 12 | "github.com/vmware/govmomi/object" 13 | "github.com/vmware/govmomi/vim25/mo" 14 | "github.com/vmware/govmomi/vim25/types" 15 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/hostsystem" 16 | ) 17 | 18 | // hostNetworkSystemFromHostSystem locates a HostNetworkSystem from a specified 19 | // HostSystem. 20 | func hostNetworkSystemFromHostSystem(hs *object.HostSystem) (*object.HostNetworkSystem, error) { 21 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 22 | defer cancel() 23 | return hs.ConfigManager().NetworkSystem(ctx) 24 | } 25 | 26 | // hostNetworkSystemFromHostSystemID locates a HostNetworkSystem from a 27 | // specified HostSystem managed object ID. 28 | func hostNetworkSystemFromHostSystemID(client *govmomi.Client, hsID string) (*object.HostNetworkSystem, error) { 29 | hs, err := hostsystem.FromID(client, hsID) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return hostNetworkSystemFromHostSystem(hs) 34 | } 35 | 36 | // hostVSwitchFromName locates a virtual switch on the supplied 37 | // HostNetworkSystem by name. 38 | func hostVSwitchFromName(client *govmomi.Client, ns *object.HostNetworkSystem, name string) (*types.HostVirtualSwitch, error) { 39 | var mns mo.HostNetworkSystem 40 | pc := client.PropertyCollector() 41 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 42 | defer cancel() 43 | if err := pc.RetrieveOne(ctx, ns.Reference(), []string{"networkInfo.vswitch"}, &mns); err != nil { 44 | return nil, fmt.Errorf("error fetching host network properties: %s", err) 45 | } 46 | 47 | for _, sw := range mns.NetworkInfo.Vswitch { 48 | if sw.Name == name { 49 | // Spec.Mtu is not set for vSwitches created directly 50 | // in ESXi. TODO: Use sw.Mtu instead of sw.Spec.Mtu in 51 | // flattenHostVirtualSwitchSpec. 52 | sw.Spec.Mtu = sw.Mtu 53 | return &sw, nil 54 | } 55 | } 56 | 57 | return nil, fmt.Errorf("could not find virtual switch %s", name) 58 | } 59 | 60 | // hostPortGroupFromName locates a port group on the supplied HostNetworkSystem 61 | // by name. 62 | func hostPortGroupFromName(client *govmomi.Client, ns *object.HostNetworkSystem, name string) (*types.HostPortGroup, error) { 63 | var mns mo.HostNetworkSystem 64 | pc := client.PropertyCollector() 65 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 66 | defer cancel() 67 | if err := pc.RetrieveOne(ctx, ns.Reference(), []string{"networkInfo.portgroup"}, &mns); err != nil { 68 | return nil, fmt.Errorf("error fetching host network properties: %s", err) 69 | } 70 | 71 | for _, pg := range mns.NetworkInfo.Portgroup { 72 | if pg.Spec.Name == name { 73 | return &pg, nil 74 | } 75 | } 76 | 77 | return nil, fmt.Errorf("could not find port group %s", name) 78 | } 79 | -------------------------------------------------------------------------------- /vsphere/host_storage_system_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "context" 9 | 10 | "github.com/vmware/govmomi" 11 | "github.com/vmware/govmomi/object" 12 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/hostsystem" 13 | ) 14 | 15 | // hostStorageSystemFromHostSystemID locates a HostStorageSystem from a 16 | // specified HostSystem managed object ID. 17 | func hostStorageSystemFromHostSystemID(client *govmomi.Client, hsID string) (*object.HostStorageSystem, error) { 18 | hs, err := hostsystem.FromID(client, hsID) 19 | if err != nil { 20 | return nil, err 21 | } 22 | ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout) 23 | defer cancel() 24 | return hs.ConfigManager().StorageSystem(ctx) 25 | } 26 | -------------------------------------------------------------------------------- /vsphere/internal/administrationroles/vsphere_permission_subresource.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package administrationroles 6 | 7 | import ( 8 | "strings" 9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 11 | ) 12 | 13 | func VspherePermissionSchema() map[string]*schema.Schema { 14 | return map[string]*schema.Schema{ 15 | "user_or_group": { 16 | Type: schema.TypeString, 17 | Required: true, 18 | Description: "User or group receiving access.", 19 | DiffSuppressFunc: func(_, old, newValue string, _ *schema.ResourceData) bool { 20 | return strings.EqualFold(old, newValue) 21 | }, 22 | }, 23 | "propagate": { 24 | Type: schema.TypeBool, 25 | Required: true, 26 | Description: "Whether or not this permission propagates down the hierarchy to sub-entities.", 27 | }, 28 | "is_group": { 29 | Type: schema.TypeBool, 30 | Required: true, 31 | Description: "Whether user_or_group field refers to a user or a group. True for a group and false for a user.", 32 | }, 33 | "role_id": { 34 | Type: schema.TypeString, 35 | Required: true, 36 | Description: "Reference to the role providing the access.", 37 | }, 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /vsphere/internal/helper/datacenter/datacenter_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package datacenter 6 | 7 | import ( 8 | "context" 9 | 10 | "github.com/vmware/govmomi" 11 | "github.com/vmware/govmomi/find" 12 | "github.com/vmware/govmomi/object" 13 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/folder" 14 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/provider" 15 | ) 16 | 17 | // FromPath returns a Datacenter via its supplied path. 18 | func FromPath(client *govmomi.Client, path string) (*object.Datacenter, error) { 19 | finder := find.NewFinder(client.Client, false) 20 | 21 | ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 22 | defer cancel() 23 | return finder.Datacenter(ctx, path) 24 | } 25 | 26 | // FromInventoryPath returns the Datacenter object which is part of a given InventoryPath 27 | func FromInventoryPath(client *govmomi.Client, inventoryPath string) (*object.Datacenter, error) { 28 | dcPath, err := folder.RootPathParticleDatastore.SplitDatacenter(inventoryPath) 29 | if err != nil { 30 | return nil, err 31 | } 32 | dc, err := FromPath(client, dcPath) 33 | if err != nil { 34 | return nil, err 35 | } 36 | 37 | return dc, nil 38 | } 39 | -------------------------------------------------------------------------------- /vsphere/internal/helper/nsx/nsx_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package nsx 6 | 7 | import ( 8 | "context" 9 | "fmt" 10 | 11 | "github.com/vmware/govmomi" 12 | "github.com/vmware/govmomi/find" 13 | "github.com/vmware/govmomi/object" 14 | "github.com/vmware/govmomi/view" 15 | "github.com/vmware/govmomi/vim25/mo" 16 | "github.com/vmware/govmomi/vim25/types" 17 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/provider" 18 | ) 19 | 20 | // OpaqueNetworkFromNetworkID looks for an opaque network via its opaque network ID. 21 | // 22 | // As NSX support in the Terraform provider is not 100% as of the time of this 23 | // writing (October 2017), this function may require some extra love in order 24 | // to be 100% functional. This is in place to support using NSX with the 25 | // vsphere_virtual_machine resource, as there is no direct path from an opaque 26 | // network backing to the managed object reference that represents the opaque 27 | // network in vCenter. 28 | func OpaqueNetworkFromNetworkID(client *govmomi.Client, id string) (*object.OpaqueNetwork, error) { 29 | // We use the same ContainerView logic that we use with networkFromID, but we 30 | // go a step further and limit it to opaque networks only. 31 | m := view.NewManager(client.Client) 32 | 33 | vctx, vcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 34 | defer vcancel() 35 | v, err := m.CreateContainerView(vctx, client.ServiceContent.RootFolder, []string{"OpaqueNetwork"}, true) 36 | if err != nil { 37 | return nil, err 38 | } 39 | 40 | defer func() { 41 | dctx, dcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 42 | defer dcancel() 43 | _ = v.Destroy(dctx) 44 | }() 45 | 46 | var networks []mo.OpaqueNetwork 47 | err = v.Retrieve(vctx, []string{"OpaqueNetwork"}, nil, &networks) 48 | if err != nil { 49 | return nil, err 50 | } 51 | 52 | for _, net := range networks { 53 | if net.Summary.(*types.OpaqueNetworkSummary).OpaqueNetworkId == id { 54 | ref := net.Reference() 55 | finder := find.NewFinder(client.Client, false) 56 | fctx, fcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 57 | 58 | nref, err := finder.ObjectReference(fctx, ref) 59 | if err != nil { 60 | fcancel() 61 | return nil, err 62 | } 63 | 64 | fcancel() 65 | 66 | // Should be safe to return here, as we have already asserted that this type 67 | // should be a OpaqueNetwork by using ContainerView, along with relying 68 | // on several fields that only an opaque network would have. 69 | return nref.(*object.OpaqueNetwork), nil 70 | } 71 | } 72 | 73 | return nil, fmt.Errorf("could not find opaque network with ID %q", id) 74 | } 75 | -------------------------------------------------------------------------------- /vsphere/internal/helper/provider/provider_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package provider 6 | 7 | import ( 8 | "fmt" 9 | "time" 10 | ) 11 | 12 | // DefaultAPITimeout is a default timeout value that is passed to functions 13 | // requiring contexts, and other various waiters. 14 | const DefaultAPITimeout = time.Minute * 5 15 | 16 | func Error(id string, function string, err error) error { 17 | return fmt.Errorf("%s: RESOURCE (%s), ACTION (%s)", err, id, function) 18 | } 19 | -------------------------------------------------------------------------------- /vsphere/internal/helper/testhelper/constants.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package testhelper 6 | 7 | const ( 8 | NfsDsName2 = "nfs-vol2" 9 | NfsPath2 = "/store" 10 | DsFolder = "ds" 11 | TestOva = "https://storage.googleapis.com/vsphere-acctest/TinyVM/TinyVM.ova" 12 | ContentLibraryFiles = "http://storage.googleapis.com/vsphere-acctest/TinyVM/TinyVM.ovf" 13 | HostNic0 = "vmnic0" 14 | HostNic1 = "vmnic1" 15 | HostNic2 = "vmnic2" 16 | ) 17 | -------------------------------------------------------------------------------- /vsphere/internal/helper/utils/entityIdToManagedObjectId.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package utils 6 | 7 | import ( 8 | "context" 9 | "log" 10 | 11 | "github.com/vmware/govmomi" 12 | "github.com/vmware/govmomi/vim25/methods" 13 | "github.com/vmware/govmomi/vim25/types" 14 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine" 15 | ) 16 | 17 | const VM = "VirtualMachine" 18 | const DISTRIBUTEDVIRTUALSWITCH = "VmwareDistributedVirtualSwitch" 19 | 20 | func GetMoid(client *govmomi.Client, entityType string, id string) (string, error) { 21 | switch entityType { 22 | case VM: 23 | vm, err := virtualmachine.FromUUID(client, id) 24 | if err != nil { 25 | log.Printf("unable to find VM object with uuid:%s, error %s,treating given id as managed object id", id, err) 26 | return id, nil 27 | } 28 | return vm.Reference().Value, nil 29 | case DISTRIBUTEDVIRTUALSWITCH: 30 | dvsm := types.ManagedObjectReference{Type: "DistributedVirtualSwitchManager", Value: "DVSManager"} 31 | req := &types.QueryDvsByUuid{ 32 | This: dvsm, 33 | Uuid: id, 34 | } 35 | resp, err := methods.QueryDvsByUuid(context.TODO(), client, req) 36 | if err != nil { 37 | log.Printf("unable to find DVS object with uuid:%s, error %s, treating given id as managed object id", id, err) 38 | return id, nil 39 | } 40 | return resp.Returnval.Reference().Value, nil 41 | default: 42 | return id, nil 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /vsphere/internal/helper/vsanclient/vsan_client_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsanclient 6 | 7 | import ( 8 | "context" 9 | 10 | "github.com/vmware/govmomi" 11 | "github.com/vmware/govmomi/object" 12 | vimtypes "github.com/vmware/govmomi/vim25/types" 13 | "github.com/vmware/govmomi/vsan" 14 | "github.com/vmware/govmomi/vsan/methods" 15 | vsantypes "github.com/vmware/govmomi/vsan/types" 16 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/provider" 17 | ) 18 | 19 | func Reconfigure(vsanClient *vsan.Client, cluster vimtypes.ManagedObjectReference, spec vsantypes.VimVsanReconfigSpec) error { 20 | ctx := context.TODO() 21 | 22 | task, err := vsanClient.VsanClusterReconfig(ctx, cluster.Reference(), spec) 23 | if err != nil { 24 | return err 25 | } 26 | return task.WaitEx(ctx) 27 | } 28 | 29 | func GetVsanConfig(vsanClient *vsan.Client, cluster vimtypes.ManagedObjectReference) (*vsantypes.VsanConfigInfoEx, error) { 30 | ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 31 | defer cancel() 32 | 33 | vsanConfig, err := vsanClient.VsanClusterGetConfig(ctx, cluster.Reference()) 34 | 35 | return vsanConfig, err 36 | } 37 | 38 | func ConvertToStretchedCluster(vsanClient *vsan.Client, client *govmomi.Client, req vsantypes.VSANVcConvertToStretchedCluster) error { 39 | ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 40 | defer cancel() 41 | 42 | res, err := methods.VSANVcConvertToStretchedCluster(ctx, vsanClient, &req) 43 | 44 | if err != nil { 45 | return err 46 | } 47 | 48 | task := object.NewTask(client.Client, res.Returnval) 49 | return task.WaitEx(ctx) 50 | } 51 | 52 | // RemoveWitnessHost removes a witness host for a stretched cluster and results in the cluster being converted 53 | // to a non-stretched cluster. 54 | func RemoveWitnessHost(vsanClient *vsan.Client, client *govmomi.Client, req vsantypes.VSANVcRemoveWitnessHost) error { 55 | ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 56 | defer cancel() 57 | 58 | res, err := methods.VSANVcRemoveWitnessHost(ctx, vsanClient, &req) 59 | 60 | if err != nil { 61 | return err 62 | } 63 | 64 | task := object.NewTask(client.Client, res.Returnval) 65 | return task.WaitEx(ctx) 66 | } 67 | 68 | func GetWitnessHosts(vsanClient *vsan.Client, cluster vimtypes.ManagedObjectReference) (*vsantypes.VSANVcGetWitnessHostsResponse, error) { 69 | ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout) 70 | defer cancel() 71 | 72 | req := vsantypes.VSANVcGetWitnessHosts{ 73 | This: vsan.VsanVcStretchedClusterSystem, 74 | Cluster: cluster.Reference(), 75 | } 76 | 77 | res, err := methods.VSANVcGetWitnessHosts(ctx, vsanClient, &req) 78 | if err != nil { 79 | return nil, err 80 | } 81 | 82 | return res, err 83 | } 84 | -------------------------------------------------------------------------------- /vsphere/internal/helper/vsansystem/vsansystem_helper.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsansystem 6 | 7 | import ( 8 | "context" 9 | "time" 10 | 11 | "github.com/vmware/govmomi" 12 | "github.com/vmware/govmomi/object" 13 | "github.com/vmware/govmomi/vim25/methods" 14 | "github.com/vmware/govmomi/vim25/mo" 15 | "github.com/vmware/govmomi/vim25/types" 16 | ) 17 | 18 | // Properties Returns the HostVsanSystem ManagedObject for the HostVsanSystem object. 19 | func Properties(hss *object.HostVsanSystem, apiTimeout time.Duration) (*mo.HostVsanSystem, error) { 20 | ctx, cancel := context.WithTimeout(context.Background(), apiTimeout) 21 | defer cancel() 22 | var hvsProps mo.HostVsanSystem 23 | err := hss.Properties(ctx, hss.Reference(), nil, &hvsProps) 24 | if err != nil { 25 | return nil, err 26 | } 27 | return &hvsProps, err 28 | } 29 | 30 | // FromHost returns a host's HostVsanSystem object. 31 | func FromHost(host *object.HostSystem, apiTimeout time.Duration) (*object.HostVsanSystem, error) { 32 | ctx, cancel := context.WithTimeout(context.Background(), apiTimeout) 33 | defer cancel() 34 | return host.ConfigManager().VsanSystem(ctx) 35 | } 36 | 37 | // RemoveDiskMapping removes the disks specified in diskMap from the disk group 38 | // on host. 39 | func RemoveDiskMapping(client *govmomi.Client, host *object.HostSystem, hvs *object.HostVsanSystem, diskMap *types.VsanHostDiskMapping, apiTimeout time.Duration) error { 40 | ctx, cancel := context.WithTimeout(context.Background(), apiTimeout) 41 | defer cancel() 42 | 43 | // If the SSD name is set, then the whole disk group needs to be removed using 44 | // RemoveDiskMapping, otherwise use RemoveDisk to just remove storage disks 45 | // from the group. 46 | if diskMap.Ssd.CanonicalName != "" { 47 | ntask := types.RemoveDiskMapping_Task{ 48 | This: hvs.Reference(), 49 | Mapping: []types.VsanHostDiskMapping{*diskMap}, 50 | } 51 | 52 | resp, err := methods.RemoveDiskMapping_Task(ctx, host.Client().RoundTripper, &ntask) 53 | if err != nil { 54 | return err 55 | } 56 | task := object.NewTask(client.Client, resp.Returnval) 57 | if err := task.WaitEx(ctx); err != nil { 58 | return err 59 | } 60 | } else { 61 | ntask := types.RemoveDisk_Task{ 62 | This: hvs.Reference(), 63 | Disk: diskMap.NonSsd, 64 | } 65 | 66 | resp, err := methods.RemoveDisk_Task(ctx, host.Client().RoundTripper, &ntask) 67 | if err != nil { 68 | return err 69 | } 70 | task := object.NewTask(client.Client, resp.Returnval) 71 | if err := task.WaitEx(ctx); err != nil { 72 | return err 73 | } 74 | } 75 | return nil 76 | } 77 | 78 | // InitializeDisks initializes and adds disks to the specified host disk group. 79 | func InitializeDisks(client *govmomi.Client, host *object.HostSystem, hvs *object.HostVsanSystem, diskMap *types.VsanHostDiskMapping, apiTimeout time.Duration) error { 80 | ctx, cancel := context.WithTimeout(context.Background(), apiTimeout) 81 | defer cancel() 82 | 83 | ntask := types.InitializeDisks_Task{ 84 | This: hvs.Reference(), 85 | Mapping: []types.VsanHostDiskMapping{*diskMap}, 86 | } 87 | 88 | resp, err := methods.InitializeDisks_Task(ctx, host.Client().RoundTripper, &ntask) 89 | if err != nil { 90 | return err 91 | } 92 | task := object.NewTask(client.Client, resp.Returnval) 93 | return task.WaitEx(ctx) 94 | } 95 | -------------------------------------------------------------------------------- /vsphere/internal/virtualdevice/virtual_machine_disk_subresource_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package virtualdevice 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/vmware/govmomi/vim25/types" 11 | ) 12 | 13 | func TestDiskCapacityInGiB(t *testing.T) { 14 | cases := []struct { 15 | name string 16 | subject *types.VirtualDisk 17 | expected int 18 | }{ 19 | { 20 | name: "capacityInBytes - integer GiB", 21 | subject: &types.VirtualDisk{ 22 | CapacityInBytes: 4294967296, 23 | CapacityInKB: 4194304, 24 | }, 25 | expected: 4, 26 | }, 27 | { 28 | name: "capacityInKB - integer GiB", 29 | subject: &types.VirtualDisk{ 30 | CapacityInKB: 4194304, 31 | }, 32 | expected: 4, 33 | }, 34 | { 35 | name: "capacityInBytes - non-integer GiB", 36 | subject: &types.VirtualDisk{ 37 | CapacityInBytes: 4294968320, 38 | CapacityInKB: 4194305, 39 | }, 40 | expected: 5, 41 | }, 42 | { 43 | name: "capacityInKB - non-integer GiB", 44 | subject: &types.VirtualDisk{ 45 | CapacityInKB: 4194305, 46 | }, 47 | expected: 5, 48 | }, 49 | } 50 | for _, tc := range cases { 51 | t.Run(tc.name, func(t *testing.T) { 52 | actual := diskCapacityInGiB(tc.subject) 53 | if tc.expected != actual { 54 | t.Fatalf("expected %d, got %d", tc.expected, actual) 55 | } 56 | }) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /vsphere/internal/virtualdevice/virtual_machine_tag_rules_subresource.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package virtualdevice 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | ) 10 | 11 | func VirtualMachineTagRulesSchema() map[string]*schema.Schema { 12 | return map[string]*schema.Schema{ 13 | "tag_category": { 14 | Type: schema.TypeString, 15 | Required: true, 16 | Description: "The tag category to select the tags from.", 17 | }, 18 | "tags": { 19 | Type: schema.TypeList, 20 | Required: true, 21 | MinItems: 1, 22 | Description: "The tags to use for creating a tag-based vm placement rule.", 23 | Elem: &schema.Schema{Type: schema.TypeString}, 24 | }, 25 | "include_datastores_with_tags": { 26 | Type: schema.TypeBool, 27 | Optional: true, 28 | Description: "Whether to include or exclude datastores tagged with the provided tags", 29 | Default: true, 30 | }, 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /vsphere/internal/vmworkflow/virtual_machine_ovfdeploy_subresource.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vmworkflow 6 | 7 | import ( 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 9 | ) 10 | 11 | func VirtualMachineOvfDeploySchema() map[string]*schema.Schema { 12 | return map[string]*schema.Schema{ 13 | "local_ovf_path": { 14 | Type: schema.TypeString, 15 | Optional: true, 16 | Description: "The absolute path to the ovf/ova file in the local system.", 17 | ForceNew: true, 18 | }, 19 | "remote_ovf_url": { 20 | Type: schema.TypeString, 21 | Optional: true, 22 | Description: "URL to the remote ovf/ova file to be deployed.", 23 | ForceNew: true, 24 | }, 25 | "ip_allocation_policy": { 26 | Type: schema.TypeString, 27 | Optional: true, 28 | Description: "The IP allocation policy.", 29 | ForceNew: true, 30 | }, 31 | "ip_protocol": { 32 | Type: schema.TypeString, 33 | Optional: true, 34 | Description: "The IP protocol.", 35 | ForceNew: true, 36 | }, 37 | "disk_provisioning": { 38 | Type: schema.TypeString, 39 | Optional: true, 40 | Description: "An optional disk provisioning. If set, all the disks in the deployed ovf will have the same specified disk type (e.g., thin provisioned).", 41 | ForceNew: true, 42 | }, 43 | "deployment_option": { 44 | Type: schema.TypeString, 45 | Optional: true, 46 | Description: "The Deployment option to be chosen. If empty, the default option is used.", 47 | ForceNew: true, 48 | }, 49 | "ovf_network_map": { 50 | Type: schema.TypeMap, 51 | Optional: true, 52 | Description: "The mapping of name of network identifiers from the ovf descriptor to network UUID in the VI infrastructure.", 53 | Elem: &schema.Schema{Type: schema.TypeString}, 54 | ForceNew: true, 55 | }, 56 | "allow_unverified_ssl_cert": { 57 | Type: schema.TypeBool, 58 | Optional: true, 59 | DefaultFunc: schema.EnvDefaultFunc("VSPHERE_ALLOW_UNVERIFIED_SSL", false), 60 | Description: "Allow unverified ssl certificates while deploying ovf/ova from url.", 61 | }, 62 | "enable_hidden_properties": { 63 | Type: schema.TypeBool, 64 | Optional: true, 65 | Default: false, 66 | Description: "Allow properties with ovf:userConfigurable=false to be set.", 67 | ForceNew: true, 68 | }, 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /vsphere/provider_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "os" 9 | "strconv" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" 13 | ) 14 | 15 | var testAccProviders map[string]*schema.Provider 16 | var testAccProvider *schema.Provider 17 | 18 | func init() { 19 | testAccProvider = Provider() 20 | testAccProviders = map[string]*schema.Provider{ 21 | "vsphere": testAccProvider, 22 | } 23 | } 24 | 25 | func TestProvider(t *testing.T) { 26 | if err := Provider().InternalValidate(); err != nil { 27 | t.Fatalf("err: %s", err) 28 | } 29 | } 30 | 31 | func testAccPreCheck(t *testing.T) { 32 | if v := os.Getenv("VSPHERE_USER"); v == "" { 33 | t.Fatal("VSPHERE_USER must be set for acceptance tests") 34 | } 35 | 36 | if v := os.Getenv("VSPHERE_PASSWORD"); v == "" { 37 | t.Fatal("VSPHERE_PASSWORD must be set for acceptance tests") 38 | } 39 | 40 | if v := os.Getenv("VSPHERE_SERVER"); v == "" { 41 | t.Fatal("VSPHERE_SERVER must be set for acceptance tests") 42 | } 43 | } 44 | 45 | func testAccSkipUnstable(t *testing.T) { 46 | if skip, _ := strconv.ParseBool(os.Getenv("TF_VAR_VSPHERE_SKIP_UNSTABLE_TESTS")); skip { 47 | t.Skip() 48 | } 49 | } 50 | 51 | func testAccCheckEnvVariables(t *testing.T, variableNames []string) { 52 | for _, name := range variableNames { 53 | if v := os.Getenv(name); v == "" { 54 | t.Skipf("%s must be set for this acceptance test", name) 55 | } 56 | } 57 | } 58 | 59 | // testAccProviderMeta returns a instantiated VSphereClient for this provider. 60 | // It's useful in state migration tests where a provider connection is actually 61 | // needed, and we don't want to go through the regular provider configure 62 | // channels (so this function doesn't interfere with the testAccProvider 63 | // package global and standard acceptance tests). 64 | // 65 | // Note we lean on environment variables for most of the provider configuration 66 | // here and this will fail if those are missing. A pre-check is not run. 67 | func testAccProviderMeta(t *testing.T) (interface{}, error) { 68 | t.Helper() 69 | d := schema.TestResourceDataRaw(t, testAccProvider.Schema, make(map[string]interface{})) 70 | return providerConfigure(d) 71 | } 72 | -------------------------------------------------------------------------------- /vsphere/resource_vsphere_entity_permissions_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "errors" 9 | "fmt" 10 | "strings" 11 | "testing" 12 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 14 | "github.com/hashicorp/terraform-plugin-testing/terraform" 15 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 16 | ) 17 | 18 | const EntityPermissionResource = "entity_permission1" 19 | 20 | func TestAccResourcevsphereEntityPermissions_basic(t *testing.T) { 21 | resource.Test(t, resource.TestCase{ 22 | PreCheck: func() { 23 | testAccPreCheck(t) 24 | }, 25 | Providers: testAccProviders, 26 | CheckDestroy: testAccResourceEntityPermissionsCheckExists(false), 27 | Steps: []resource.TestStep{ 28 | { 29 | Config: testAccResourceVsphereEntityPermissionsConfigBasic(), 30 | Check: resource.ComposeTestCheckFunc( 31 | testAccResourceEntityPermissionsCheckExists(true), 32 | resource.TestCheckResourceAttrSet("vsphere_entity_permissions."+EntityPermissionResource, "permissions.0.user_or_group"), 33 | resource.TestCheckResourceAttr("vsphere_entity_permissions."+EntityPermissionResource, "permissions.0.propagate", "true"), 34 | resource.TestCheckResourceAttr("vsphere_entity_permissions."+EntityPermissionResource, "permissions.0.is_group", "true"), 35 | ), 36 | }, 37 | }, 38 | }) 39 | } 40 | 41 | func testAccResourceEntityPermissionsCheckExists(expected bool) resource.TestCheckFunc { 42 | return func(s *terraform.State) error { 43 | _, err := testGetVsphereEntityPermission(s, EntityPermissionResource) 44 | if err != nil { 45 | if strings.Contains(err.Error(), "permissions not found") && !expected { 46 | // Expected missing 47 | return nil 48 | } 49 | return err 50 | } 51 | if !expected { 52 | return errors.New("expected permissions to be missing") 53 | } 54 | return nil 55 | } 56 | } 57 | 58 | func testAccResourceVsphereEntityPermissionsConfigBasic() string { 59 | return fmt.Sprintf(` 60 | %s 61 | 62 | data "vsphere_role" "role1" { 63 | label = "Administrator" 64 | } 65 | 66 | resource vsphere_entity_permissions "%s" { 67 | entity_id = data.vsphere_datacenter.rootdc1.id 68 | entity_type = "Datacenter" 69 | permissions { 70 | user_or_group = "%s" 71 | propagate = true 72 | is_group = true 73 | role_id = data.vsphere_role.role1.id 74 | } 75 | } 76 | `, testhelper.ConfigDataRootDC1(), 77 | EntityPermissionResource, 78 | "root", 79 | ) 80 | } 81 | -------------------------------------------------------------------------------- /vsphere/resource_vsphere_offline_software_depot_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "fmt" 9 | "os" 10 | "testing" 11 | 12 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 13 | "github.com/hashicorp/terraform-plugin-testing/terraform" 14 | "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/testhelper" 15 | ) 16 | 17 | func TestAccResourceVSphereOfflineSoftwareDepot_basic(t *testing.T) { 18 | testAccSkipUnstable(t) 19 | resource.Test(t, resource.TestCase{ 20 | PreCheck: func() { 21 | RunSweepers() 22 | testAccPreCheck(t) 23 | testAccResourceVSphereOfflineSoftwareDepotPreCheck(t) 24 | }, 25 | Providers: testAccProviders, 26 | Steps: []resource.TestStep{ 27 | { 28 | Config: testhelper.ConfigDataSoftwareDepot(), 29 | Check: resource.ComposeTestCheckFunc( 30 | testAccResourceVSphereOfflineSoftwareDepotCheckFunc(), 31 | ), 32 | }, 33 | }, 34 | }) 35 | } 36 | 37 | func testAccResourceVSphereOfflineSoftwareDepotCheckFunc() resource.TestCheckFunc { 38 | return func(s *terraform.State) error { 39 | tVars, err := testClientVariablesForResource(s, "vsphere_offline_software_depot.depot") 40 | if err != nil { 41 | return err 42 | } 43 | 44 | location := tVars.resourceAttributes["location"] 45 | expected := os.Getenv("TF_VAR_VSPHERE_SOFTWARE_DEPOT_LOCATION") 46 | if location != expected { 47 | return fmt.Errorf("depot location is incorrect. Expected %s but got %s", expected, location) 48 | } 49 | 50 | return nil 51 | } 52 | } 53 | 54 | func testAccResourceVSphereOfflineSoftwareDepotPreCheck(t *testing.T) { 55 | if os.Getenv("TF_VAR_VSPHERE_SOFTWARE_DEPOT_LOCATION") == "" { 56 | t.Skip("set TF_VAR_VSPHERE_SOFTWARE_DEPOT_LOCATION to run vsphere_offline_software_depot acceptance tests") 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /vsphere/resource_vsphere_virtual_machine_class_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "testing" 9 | 10 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 11 | ) 12 | 13 | func TestAccResourceVSphereVmClass_basic(t *testing.T) { 14 | testAccSkipUnstable(t) 15 | resource.Test(t, resource.TestCase{ 16 | PreCheck: func() { 17 | testAccPreCheck(t) 18 | }, 19 | Providers: testAccProviders, 20 | Steps: []resource.TestStep{ 21 | { 22 | Config: testAccVSphereVMClassConfig(), 23 | Check: resource.ComposeTestCheckFunc( 24 | resource.TestCheckResourceAttrSet("vsphere_virtual_machine_class.vm_class_1", "id"), 25 | ), 26 | }, 27 | }, 28 | }) 29 | } 30 | 31 | func TestAccResourceVSphereVmClass_vgpu(t *testing.T) { 32 | testAccSkipUnstable(t) 33 | resource.Test(t, resource.TestCase{ 34 | PreCheck: func() { 35 | testAccPreCheck(t) 36 | }, 37 | Providers: testAccProviders, 38 | Steps: []resource.TestStep{ 39 | { 40 | Config: testAccVSphereVMClassConfigVgpu(), 41 | Check: resource.ComposeTestCheckFunc( 42 | resource.TestCheckResourceAttrSet("vsphere_virtual_machine_class.vm_class_1", "id"), 43 | ), 44 | }, 45 | }, 46 | }) 47 | } 48 | 49 | func testAccVSphereVMClassConfig() string { 50 | return ` 51 | resource "vsphere_virtual_machine_class" "vm_class_1" { 52 | name = "test-class-11" 53 | cpus = 4 54 | memory = 4096 55 | memory_reservation = 100 56 | } 57 | ` 58 | } 59 | 60 | func testAccVSphereVMClassConfigVgpu() string { 61 | return ` 62 | resource "vsphere_virtual_machine_class" "vm_class_1" { 63 | name = "test-class-11" 64 | cpus = 4 65 | memory = 4096 66 | memory_reservation = 100 67 | vgpu_devices = ["mockup-vmiop"] 68 | } 69 | ` 70 | } 71 | -------------------------------------------------------------------------------- /vsphere/testdata/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmware/terraform-provider-vsphere/db21a080528bb11f88cec5f8a52fb23622f888d4/vsphere/testdata/.gitkeep -------------------------------------------------------------------------------- /vsphere/util.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | func stringInSlice(s string, list []string) bool { 8 | for _, i := range list { 9 | if i == s { 10 | return true 11 | } 12 | } 13 | return false 14 | } 15 | -------------------------------------------------------------------------------- /vsphere/vsphere_sweep_test.go: -------------------------------------------------------------------------------- 1 | // © Broadcom. All Rights Reserved. 2 | // The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. 3 | // SPDX-License-Identifier: MPL-2.0 4 | 5 | package vsphere 6 | 7 | import ( 8 | "os" 9 | "testing" 10 | 11 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" 12 | ) 13 | 14 | func TestMain(m *testing.M) { 15 | resource.TestMain(m) 16 | } 17 | 18 | func sweepVSphereClient() (*Client, error) { 19 | config := Config{ 20 | InsecureFlag: true, 21 | Debug: false, 22 | Persist: true, 23 | User: os.Getenv("VSPHERE_USER"), 24 | Password: os.Getenv("VSPHERE_PASSWORD"), 25 | VSphereServer: os.Getenv("VSPHERE_SERVER"), 26 | DebugPath: "", 27 | DebugPathRun: "", 28 | VimSessionPath: "", 29 | RestSessionPath: "", 30 | KeepAlive: 0, 31 | } 32 | return config.Client() 33 | } 34 | --------------------------------------------------------------------------------