Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(workspace/app): add data source to get list of the image servers #5922

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions docs/data-sources/workspace_app_image_servers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
subcategory: "Workspace"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_workspace_app_image_servers"
description: |-
Use this data source to get the list of the image servers within HuaweiCloud.
---

# huaweicloud_workspace_app_image_servers

Use this data source to get the list of the image servers within HuaweiCloud.

## Example Usage

### Query all image servers

```hcl
data "huaweicloud_workspace_app_image_servers" "test" {}
```

### Query image server with the specified image server ID

```hcl
variable "image_server_id" {}

data "huaweicloud_workspace_app_image_servers" "test" {
server_id = var.image_server_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

* `name` - (Optional, String) Specified the name of the image server.
Fuzzy search is supported.

* `server_id` - (Optional, String) Specified the ID of the image server.

* `enterprise_project_id` - (Optional, String) Specifies the ID of the enterprise project to which the image server belongs.
This parameter is only valid for enterprise users, if omitted, all enterprise project IDs will be queried.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `servers` - All image servers that match the filter parameters.

The [servers](#app_image_servers) structure is documented below.

<a name="app_image_servers"></a>
The `servers` block supports:

* `id` - The ID of the image server.

* `name` - The name of the image server.

* `description` - The description of the image server.

* `image_generated_product_id` - The ID of the generated image product.

* `image_id` - The ID of the basic image to which the image server belongs.

* `image_type` - The type of the basic image to which the image server belongs.
+ **gold**: The market image.
+ **public**: The public image.
+ **private**: The private image.
+ **shared**: The shared image.
+ **other**

* `spce_code` - The specification code of the basic image to which the image server belongs.

* `aps_server_group_id` - The ID of the APS server group associated with the image server.

* `aps_server_id` - The ID of the APS server associated with the image server.

* `app_group_id` - The ID of the application group associated with the image server.

* `status` - The current status of the image server.
+ **ACTIVE**
+ **BUILT**: Image task is finished.
+ **ERROR**

* `authorize_accounts` - The list of authorized users of the application group associated with the image server.

The [authorize_accounts](#servers_authorize_accounts_struct) structure is documented below.

* `enterprise_project_id` - The enterprise project ID to which the image server belongs.

* `created_at` - The creation time of the image server, in RFC3339 format.

* `updated_at` - The latest update time of the image server, in RFC3339 format.

<a name="servers_authorize_accounts_struct"></a>
The `authorize_accounts` block supports:

* `account` - The name of the account.

* `type` - The type of the account.

* `domain` - The domain name of the Workspace service.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ func Provider() *schema.Provider {

"huaweicloud_workspace_app_group_authorizations": workspace.DataSourceWorkspaceAppGroupAuthorizations(),
"huaweicloud_workspace_app_groups": workspace.DataSourceWorkspaceAppGroups(),
"huaweicloud_workspace_app_image_servers": workspace.DataSourceWorkspaceAppImageServers(),
"huaweicloud_workspace_app_nas_storages": workspace.DataSourceAppNasStorages(),
"huaweicloud_workspace_app_publishable_apps": workspace.DataSourceWorkspaceAppPublishableApps(),
"huaweicloud_workspace_app_storage_policies": workspace.DataSourceAppStoragePolicies(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package workspace

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceAppImageServers_basic(t *testing.T) {
var (
dataSource = "data.huaweicloud_workspace_app_image_servers.test"
rName = acceptance.RandomAccResourceName()
dc = acceptance.InitDataSourceCheck(dataSource)

byServerId = "data.huaweicloud_workspace_app_image_servers.filter_by_server_id"
dcByServerId = acceptance.InitDataSourceCheck(byServerId)

byName = "data.huaweicloud_workspace_app_image_servers.filter_by_name"
dcByName = acceptance.InitDataSourceCheck(byName)

byEpsId = "data.huaweicloud_workspace_app_image_servers.filter_by_eps_id"
dcByEpsId = acceptance.InitDataSourceCheck(byEpsId)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckWorkspaceAppServerGroup(t)
acceptance.TestAccPreCheckWorkspaceAppImageSpecCode(t)
acceptance.TestAccPrecheckWorkspaceUserNames(t)
acceptance.TestAccPreCheckWorkspaceOUName(t)
acceptance.TestAccPreCheckWorkspaceADDomainNames(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceAppImageServers_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestMatchResourceAttr(dataSource, "servers.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
dcByServerId.CheckResourceExists(),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.image_id"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.image_type"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.spce_code"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.aps_server_group_id"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.aps_server_id"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.app_group_id"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.status"),
resource.TestCheckResourceAttr(byServerId, "servers.0.authorize_accounts.#", "1"),
resource.TestCheckResourceAttr(byServerId, "servers.0.authorize_accounts.0.type", "USER"),
resource.TestCheckResourceAttrSet(byServerId, "servers.0.authorize_accounts.0.domain"),
resource.TestCheckOutput("is_server_id_filter_useful", "true"),
resource.TestMatchResourceAttr(byServerId, "servers.0.created_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
resource.TestMatchResourceAttr(byServerId, "servers.0.updated_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
dcByName.CheckResourceExists(),
resource.TestCheckOutput("is_name_filter_useful", "true"),
dcByEpsId.CheckResourceExists(),
resource.TestCheckOutput("is_eps_id_filter_useful", "true"),
),
},
},
})
}

func testDataSourceAppImageServers_basic(name string) string {
return fmt.Sprintf(`
%[1]s

data "huaweicloud_workspace_app_image_servers" "test" {
depends_on = [huaweicloud_workspace_app_image_server.test]
}

locals {
server_id = huaweicloud_workspace_app_image_server.test.id
server_name = huaweicloud_workspace_app_image_server.test.name
eps_id = huaweicloud_workspace_app_image_server.test.enterprise_project_id
}

data "huaweicloud_workspace_app_image_servers" "filter_by_server_id" {
depends_on = [huaweicloud_workspace_app_image_server.test]

server_id = local.server_id
}

locals {
server_id_filter_result = [for v in data.huaweicloud_workspace_app_image_servers.filter_by_server_id.servers[*].id :
v == local.server_id]
}

output "is_server_id_filter_useful" {
value = length(local.server_id_filter_result) == 1 && alltrue(local.server_id_filter_result)
}

# Fuzzy search is supported.
data "huaweicloud_workspace_app_image_servers" "filter_by_name" {
depends_on = [huaweicloud_workspace_app_image_server.test]

name = local.server_name
}

locals {
name_filter_result = [for v in data.huaweicloud_workspace_app_image_servers.filter_by_name.servers : strcontains(v.name, local.server_name)]
}

output "is_name_filter_useful" {
value = length(local.name_filter_result) > 0 && alltrue(local.name_filter_result)
}

data "huaweicloud_workspace_app_image_servers" "filter_by_eps_id" {
depends_on = [huaweicloud_workspace_app_image_server.test]

enterprise_project_id = local.eps_id
}

locals {
eps_id_filter_result = [for v in data.huaweicloud_workspace_app_image_servers.filter_by_eps_id.servers[*].enterprise_project_id :
v == local.eps_id]
}

output "is_eps_id_filter_useful" {
value = length(local.eps_id_filter_result) > 0 && alltrue(local.eps_id_filter_result)
}
`, testResourceAppImageServer_basic(name, "Data source test"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ resource "huaweicloud_workspace_app_image_server" "test" {
authorize_accounts {
account = split(",", "%[8]s")[0]
type = "USER"
domain = element(split("%[9]s", ","), 0)
domain = element(split(",", "%[9]s"), 0)
}

root_volume {
Expand Down
Loading
Loading