Skip to content
Open
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
79 changes: 79 additions & 0 deletions docs/data-sources/swrv3_image_tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
subcategory: "Software Repository for Container (SWR)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_swrv3_image_tags"
description: |-
Use this data source to get the list of SWR image tags.
---

# huaweicloud_swrv3_image_tags

Use this data source to get the list of SWR image tags.

## Example Usage

```hcl
variable "organization" {}
variable "repository" {}

data "huaweicloud_swrv3_image_tags" "test" {
organization = var.organization
repository = var.repository
}
```

## 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.

* `organization` - (Required, String) Specifies the name of the organization.

* `repository` - (Required, String) Specifies the name of the repository.

* `tag` - (Optional, String) Specifies the source tag.

* `with_manifest` - (Optional, Bool) Specifies whether to get the manifest infos.

## Attribute Reference

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

* `id` - The data source ID.

* `tags` - Indicates the tag list.

The [tags](#tags_struct) structure is documented below.

<a name="tags_struct"></a>
The `tags` block supports:

* `id` - Indicates the tag ID.

* `schema` - Indicates the docker schema.

* `size` - Indicates the image size.

* `created` - Indicates the last update time.

* `updated` - Indicates the last update time.

* `tag` - Indicates the image tag.

* `digest` - Indicates the image digest.

* `path` - Indicates the image pull path.

* `internal_path` - Indicates the image internal pull path.

* `repo_id` - Indicates the repository ID.

* `image_id` - Indicates the image ID.

* `manifest` - Indicates the image manifest.

* `is_trusted` - Indicates whether the image is trusted.

* `tag_type` - Indicates the tag type.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,7 @@ func Provider() *schema.Provider {
"huaweicloud_swrv3_repositories": swr.DataSourceSwrv3Repositories(),
"huaweicloud_swr_shared_repositories": swr.DataSourceSharedRepositories(),
"huaweicloud_swrv3_shared_repositories": swr.DataSourceSwrv3SharedRepositories(),
"huaweicloud_swrv3_image_tags": swr.DataSourceSwrv3ImageTags(),
"huaweicloud_swr_image_auto_sync_jobs": swr.DataSourceSwrImageAutoSyncJobs(),
"huaweicloud_swr_image_triggers": swr.DataSourceImageTriggers(),
"huaweicloud_swr_image_tags": swr.DataSourceImageTags(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package swr

import (
"fmt"
"testing"

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

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

func TestAccDataSourceSwrv3ImageTags_basic(t *testing.T) {
dataSource := "data.huaweicloud_swrv3_image_tags.test"
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckSwrRepository(t)
acceptance.TestAccPreCheckSwrOrigination(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceSwrv3ImageTags_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "tags.#"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.id"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.schema"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.size"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.tag"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.digest"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.path"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.internal_path"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.repo_id"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.image_id"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.created"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.updated"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.manifest"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.is_trusted"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.tag_type"),

resource.TestCheckOutput("tag_filter_is_useful", "true"),
),
},
},
})
}

func testDataSourceSwrv3ImageTags_basic() string {
return fmt.Sprintf(`
data "huaweicloud_swrv3_image_tags" "test" {
organization = "%[1]s"
repository = "%[2]s"
with_manifest = true
}

data "huaweicloud_swrv3_image_tags" "filter_by_tag" {
organization = "%[1]s"
repository = "%[2]s"
tag = data.huaweicloud_swrv3_image_tags.test.tags[0].tag
}

output "tag_filter_is_useful" {
value = length(data.huaweicloud_swrv3_image_tags.filter_by_tag.tags) == 1
}
`, acceptance.HW_SWR_ORGANIZATION, acceptance.HW_SWR_REPOSITORY)
}
216 changes: 216 additions & 0 deletions huaweicloud/services/swr/data_source_huaweicloud_swrv3_image_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Generated by PMS #1014
package swr

import (
"context"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceSwrv3ImageTags() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSwrv3ImageTagsRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"organization": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the name of the organization.`,
},
"repository": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the name of the repository.`,
},
"tag": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the source tag.`,
},
"with_manifest": {
Type: schema.TypeBool,
Optional: true,
Description: `Specifies whether to get the manifest infos.`,
},
"tags": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the tag list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
Description: `Indicates the tag ID.`,
},
"schema": {
Type: schema.TypeInt,
Computed: true,
Description: `Indicates the docker schema.`,
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: `Indicates the image size.`,
},
"created": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the last update time.`,
},
"updated": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the last update time.`,
},
"tag": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image tag.`,
},
"digest": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image digest.`,
},
"path": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image pull path.`,
},
"internal_path": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image internal pull path.`,
},
"repo_id": {
Type: schema.TypeInt,
Computed: true,
Description: `Indicates the repository ID.`,
},
"image_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image ID.`,
},
"manifest": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the image manifest.`,
},
"is_trusted": {
Type: schema.TypeBool,
Computed: true,
Description: `Indicates whether the image is trusted.`,
},
"tag_type": {
Type: schema.TypeInt,
Computed: true,
Description: `Indicates the tag type.`,
},
},
},
},
},
}
}

type v3ImageTagsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newv3ImageTagsDSWrapper(d *schema.ResourceData, meta interface{}) *v3ImageTagsDSWrapper {
return &v3ImageTagsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceSwrv3ImageTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newv3ImageTagsDSWrapper(d, meta)
listRepositoryTagRst, err := wrapper.ListRepositoryTag()
if err != nil {
return diag.FromErr(err)
}

err = wrapper.listRepositoryTagToSchema(listRepositoryTagRst)
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
return nil
}

// @API SWR GET /v3/manage/namespaces/{namespace}/repos/{repository}/tags
func (w *v3ImageTagsDSWrapper) ListRepositoryTag() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "swr")
if err != nil {
return nil, err
}

uri := "/v3/manage/namespaces/{namespace}/repos/{repository}/tags"
uri = strings.ReplaceAll(uri, "{namespace}", w.Get("organization").(string))
uri = strings.ReplaceAll(uri, "{repository}", w.Get("repository").(string))
params := map[string]any{
"tag": w.Get("tag"),
"with_manifest": w.Get("with_manifest"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
MarkerPager("tags", "next_marker", "marker").
Request().
Result()
}

func (w *v3ImageTagsDSWrapper) listRepositoryTagToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("tags", schemas.SliceToList(body.Get("tags"),
func(tags gjson.Result) any {
return map[string]any{
"id": tags.Get("id").Value(),
"schema": tags.Get("schema").Value(),
"size": tags.Get("size").Value(),
"created": tags.Get("created").Value(),
"updated": tags.Get("updated").Value(),
"tag": tags.Get("tag").Value(),
"digest": tags.Get("digest").Value(),
"path": tags.Get("path").Value(),
"internal_path": tags.Get("internal_path").Value(),
"repo_id": tags.Get("repo_id").Value(),
"image_id": tags.Get("image_id").Value(),
"manifest": tags.Get("manifest").Value(),
"is_trusted": tags.Get("is_trusted").Value(),
"tag_type": tags.Get("tag_type").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading