Skip to content
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
2 changes: 2 additions & 0 deletions docs/data-sources/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data "rhcs_versions" "all" {}

Read-Only:

- `available_channels` (List of String) Update channels in which this version is present, for example 'stable-4.20'
- `id` (String) Unique identifier of the version. This is what should be used when referencing the versions from other places, for example in the 'version' attribute of the cluster resource.
- `name` (String) Short name of the version, for example '4.1.0'.

Expand All @@ -43,6 +44,7 @@ Read-Only:

Read-Only:

- `available_channels` (List of String) Update channels in which this version is present, for example 'stable-4.20'
- `id` (String) Unique identifier of the version. This is what should be used when referencing the versions from other places, for example in the 'version' attribute of the cluster resource.
- `name` (String) Short name of the version, for example '4.1.0'.

Expand Down
19 changes: 17 additions & 2 deletions provider/versions/versions_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/common"
)

type VersionsDataSource struct {
Expand Down Expand Up @@ -81,6 +82,11 @@ func (s *VersionsDataSource) itemAttributes() map[string]schema.Attribute {
Description: "Short name of the version, for example '4.1.0'.",
Computed: true,
},
"available_channels": schema.ListAttribute{
ElementType: types.StringType,
Description: "Update channels in which this version is present, for example 'stable-4.20'",
Computed: true,
},
}
}

Expand Down Expand Up @@ -145,9 +151,18 @@ func (s *VersionsDataSource) Read(ctx context.Context, req datasource.ReadReques
// Populate the state:
state.Items = make([]*VersionState, len(listItems))
for i, listItem := range listItems {
availableChannels, err := common.StringArrayToList(listItem.AvailableChannels())
if err != nil {
resp.Diagnostics.AddError(
"Can't extract available channels from version object",
err.Error(),
)
return
}
state.Items[i] = &VersionState{
ID: types.StringValue(listItem.ID()),
Name: types.StringValue(listItem.RawID()),
ID: types.StringValue(listItem.ID()),
Name: types.StringValue(listItem.RawID()),
AvailableChannels: availableChannels,
}
}
if len(state.Items) == 1 {
Expand Down
5 changes: 3 additions & 2 deletions provider/versions/versions_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type VersionsState struct {
}

type VersionState struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
AvailableChannels types.List `tfsdk:"available_channels"`
}
97 changes: 90 additions & 7 deletions subsystem/classic/versions_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ var _ = Describe("Versions data source", func() {
"items": [
{
"id": "openshift-v4.8.1",
"raw_id": "4.8.1"
"raw_id": "4.8.1",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
},
{
"id": "openshift-v4.8.2",
"raw_id": "4.8.2"
"raw_id": "4.8.2",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
}
]
}`),
Expand All @@ -64,8 +66,81 @@ var _ = Describe("Versions data source", func() {
Expect(resource).To(MatchJQ(`.attributes.items | length`, 2))
Expect(resource).To(MatchJQ(`.attributes.items[0].id`, "openshift-v4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.items[0].name`, "4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.items[0].available_channels`, []any{"stable-4.8", "eus-4.8", "fast-4.8"}))
Expect(resource).To(MatchJQ(`.attributes.items[1].id`, "openshift-v4.8.2"))
Expect(resource).To(MatchJQ(`.attributes.items[1].name`, "4.8.2"))
Expect(resource).To(MatchJQ(`.attributes.items[1].available_channels`, []any{"stable-4.8", "eus-4.8", "fast-4.8"}))
})

It("Can handle missing available_channels field", func() {
// Prepare the server:
TestServer.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/versions"),
VerifyFormKV("search", "enabled = 't'"),
RespondWithJSON(http.StatusOK, `{
"page": 1,
"size": 1,
"total": 1,
"items": [
{
"id": "openshift-v4.8.1",
"raw_id": "4.8.1"
}
]
}`),
),
)

// Run the apply command:
Terraform.Source(`
data "rhcs_versions" "my_versions" {
}
`)
runOutput := Terraform.Apply()
Expect(runOutput.ExitCode).To(BeZero())

// Check the state:
resource := Terraform.Resource("rhcs_versions", "my_versions")
Expect(resource).To(MatchJQ(`.attributes.item.id`, "openshift-v4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.name`, "4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.available_channels`, []any{}))
})

It("Can handle empty available_channels field", func() {
// Prepare the server:
TestServer.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/versions"),
VerifyFormKV("search", "enabled = 't'"),
RespondWithJSON(http.StatusOK, `{
"page": 1,
"size": 1,
"total": 1,
"items": [
{
"id": "openshift-v4.8.1",
"raw_id": "4.8.1",
"available_channels": []
}
]
}`),
),
)

// Run the apply command:
Terraform.Source(`
data "rhcs_versions" "my_versions" {
}
`)
runOutput := Terraform.Apply()
Expect(runOutput.ExitCode).To(BeZero())

// Check the state:
resource := Terraform.Resource("rhcs_versions", "my_versions")
Expect(resource).To(MatchJQ(`.attributes.item.id`, "openshift-v4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.name`, "4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.available_channels`, []any{}))
})

It("Can search versions", func() {
Expand All @@ -82,11 +157,13 @@ var _ = Describe("Versions data source", func() {
"items": [
{
"id": "openshift-v4.8.1-fast",
"raw_id": "4.8.1"
"raw_id": "4.8.1",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
},
{
"id": "openshift-v4.8.2-fast",
"raw_id": "4.8.2"
"raw_id": "4.8.2",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
}
]
}`),
Expand All @@ -108,8 +185,10 @@ var _ = Describe("Versions data source", func() {
Expect(resource).To(MatchJQ(`.attributes.items | length`, 2))
Expect(resource).To(MatchJQ(`.attributes.items[0].id`, "openshift-v4.8.1-fast"))
Expect(resource).To(MatchJQ(`.attributes.items[0].name`, "4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.items[0].available_channels`, []any{"stable-4.8", "eus-4.8", "fast-4.8"}))
Expect(resource).To(MatchJQ(`.attributes.items[1].id`, "openshift-v4.8.2-fast"))
Expect(resource).To(MatchJQ(`.attributes.items[1].name`, "4.8.2"))
Expect(resource).To(MatchJQ(`.attributes.items[1].available_channels`, []any{"stable-4.8", "eus-4.8", "fast-4.8"}))
})

It("Populates `item` if there is exactly one result", func() {
Expand All @@ -124,7 +203,8 @@ var _ = Describe("Versions data source", func() {
"items": [
{
"id": "openshift-v4.8.1",
"raw_id": "4.8.1"
"raw_id": "4.8.1",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
}
]
}`),
Expand All @@ -143,6 +223,7 @@ var _ = Describe("Versions data source", func() {
resource := Terraform.Resource("rhcs_versions", "my_versions")
Expect(resource).To(MatchJQ(`.attributes.item.id`, "openshift-v4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.name`, "4.8.1"))
Expect(resource).To(MatchJQ(`.attributes.item.available_channels`, []any{"stable-4.8", "eus-4.8", "fast-4.8"}))
})

It("Doesn't populate `item` if there are zero results", func() {
Expand Down Expand Up @@ -184,11 +265,13 @@ var _ = Describe("Versions data source", func() {
"items": [
{
"id": "openshift-v4.8.1",
"raw_id": "4.8.1"
"raw_id": "4.8.1",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
},
{
"id": "openshift-v4.8.2",
"raw_id": "4.8.2"
"raw_id": "4.8.2",
"available_channels": ["stable-4.8", "eus-4.8", "fast-4.8"]
}
]
}`),
Expand Down
Loading