Skip to content

Commit

Permalink
chore: refactor name and parameter for classic/hcp
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Dec 4, 2024
1 parent 3d6578f commit 011f585
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *ClusterRosaClassicDatasource) Read(ctx context.Context, request datasou
object := get.Body()

// Save the state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down
46 changes: 22 additions & 24 deletions provider/clusterrosa/classic/cluster_rosa_classic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,8 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
tflog.Debug(ctx, "begin create()")

// Get the plan:
state := &ClusterRosaClassicState{}
diags := request.Plan.Get(ctx, state)
plan := &ClusterRosaClassicState{}
diags := request.Plan.Get(ctx, plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
Expand All @@ -805,7 +805,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc

// In case version with "openshift-v" prefix was used here,
// Give a meaningful message to inform the user that it not supported any more
if common.HasValue(state.Version) && strings.HasPrefix(state.Version.ValueString(), rosa.VersionPrefix) {
if common.HasValue(plan.Version) && strings.HasPrefix(plan.Version.ValueString(), rosa.VersionPrefix) {
response.Diagnostics.AddError(
summary,
"Openshift version must be provided without the \"openshift-v\" prefix",
Expand All @@ -814,44 +814,44 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
}

channelGroup := consts.DefaultChannelGroup
if common.HasValue(state.ChannelGroup) {
channelGroup = state.ChannelGroup.ValueString()
if common.HasValue(plan.ChannelGroup) {
channelGroup = plan.ChannelGroup.ValueString()
}
desiredVersion := ""
if common.HasValue(state.Version) {
desiredVersion = state.Version.ValueString()
if common.HasValue(plan.Version) {
desiredVersion = plan.Version.ValueString()
}
version, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Classic, channelGroup, desiredVersion)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

err = validateHttpTokensVersion(ctx, state, version)
err = validateHttpTokensVersion(ctx, plan, version)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

object, err := createClassicClusterObject(ctx, state, diags)
object, err := createClassicClusterObject(ctx, plan, diags)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
Expand All @@ -863,15 +863,15 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
summary,
fmt.Sprintf(
"Can't create cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}
object = add.Body()

// Save initial state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -881,24 +881,24 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, plan)...)

if common.HasValue(state.WaitForCreateComplete) && state.WaitForCreateComplete.ValueBool() {
if common.HasValue(plan.WaitForCreateComplete) && plan.WaitForCreateComplete.ValueBool() {
object, err = r.ClusterWait.WaitForClusterToBeReady(ctx, object.ID(), rosa.DefaultWaitTimeoutInMinutes)
if err != nil {
response.Diagnostics.AddError(
"Waiting for cluster creation finished with error",
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
}

// Save the state post wait completion:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -908,9 +908,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
)
return
}

diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
}

func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.ReadRequest,
Expand Down Expand Up @@ -947,7 +945,7 @@ func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.
object := get.Body()

// Save the state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -1140,7 +1138,7 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request resourc
object := update.Body()

// Update the state:
err = populateRosaClassicClusterState(ctx, object, plan, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -1412,7 +1410,7 @@ func (r *ClusterRosaClassicResource) ImportState(ctx context.Context, request re
}

// populateRosaClassicClusterState copies the data from the API object to the Terraform state.
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState, httpClient common.HttpClient) error {
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState) error {
state.ID = types.StringValue(object.ID())
state.ExternalID = types.StringValue(object.ExternalID())
object.API()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {

clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)).To(Succeed())
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)).To(Succeed())

Expect(clusterState.ID.ValueString()).To(Equal(clusterId))
Expect(clusterState.CloudRegion.ValueString()).To(Equal(regionId))
Expand Down Expand Up @@ -281,7 +281,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())

err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
Expect(err).ToNot(HaveOccurred())
Expect(clusterState.Sts.OIDCEndpointURL.ValueString()).To(Equal("nonce.com"))
})
Expand All @@ -296,7 +296,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())

err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
Expect(err).ToNot(HaveOccurred())
Expect(clusterState.Sts.Thumbprint.ValueString()).To(Equal(""))
})
Expand Down
45 changes: 21 additions & 24 deletions provider/clusterrosa/hcp/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,16 +717,16 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
tflog.Debug(ctx, "begin create()")

// Get the plan:
state := &ClusterRosaHcpState{}
diags := request.Plan.Get(ctx, state)
plan := &ClusterRosaHcpState{}
diags := request.Plan.Get(ctx, plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
summary := "Can't build cluster"

shouldWaitCreationComplete := common.BoolWithFalseDefault(state.WaitForCreateComplete)
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(state.WaitForStdComputeNodesComplete)
shouldWaitCreationComplete := common.BoolWithFalseDefault(plan.WaitForCreateComplete)
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(plan.WaitForStdComputeNodesComplete)
if shouldWaitComputeNodesComplete && !shouldWaitCreationComplete {
response.Diagnostics.AddError(
summary,
Expand All @@ -735,8 +735,8 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
return
}

hasEtcdEncrpytion := common.BoolWithFalseDefault(state.EtcdEncryption)
hasEtcdKmsKeyArn := common.HasValue(state.EtcdKmsKeyArn) && state.EtcdKmsKeyArn.ValueString() != ""
hasEtcdEncrpytion := common.BoolWithFalseDefault(plan.EtcdEncryption)
hasEtcdKmsKeyArn := common.HasValue(plan.EtcdKmsKeyArn) && plan.EtcdKmsKeyArn.ValueString() != ""
if (!hasEtcdEncrpytion && hasEtcdKmsKeyArn) || (hasEtcdEncrpytion && !hasEtcdKmsKeyArn) {
response.Diagnostics.AddError(
summary,
Expand All @@ -747,7 +747,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr

// In case version with "openshift-v" prefix was used here,
// Give a meaningful message to inform the user that it not supported any more
if common.HasValue(state.Version) && strings.HasPrefix(state.Version.ValueString(), rosa.VersionPrefix) {
if common.HasValue(plan.Version) && strings.HasPrefix(plan.Version.ValueString(), rosa.VersionPrefix) {
response.Diagnostics.AddError(
summary,
"Openshift version must be provided without the \"openshift-v\" prefix",
Expand All @@ -756,32 +756,32 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
}

channelGroup := consts.DefaultChannelGroup
if common.HasValue(state.ChannelGroup) {
channelGroup = state.ChannelGroup.ValueString()
if common.HasValue(plan.ChannelGroup) {
channelGroup = plan.ChannelGroup.ValueString()
}
desiredVersion := ""
if common.HasValue(state.Version) {
desiredVersion = state.Version.ValueString()
if common.HasValue(plan.Version) {
desiredVersion = plan.Version.ValueString()
}
_, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Hcp, channelGroup, desiredVersion)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

object, err := createHcpClusterObject(ctx, state, diags)
object, err := createHcpClusterObject(ctx, plan, diags)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
Expand All @@ -793,15 +793,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
summary,
fmt.Sprintf(
"Can't create cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}
object = add.Body()

// Save initial state:
err = populateRosaHcpClusterState(ctx, object, state)
err = populateRosaHcpClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -811,6 +811,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, plan)...)

if shouldWaitCreationComplete {
tflog.Info(ctx, "Waiting for cluster to get ready")
Expand All @@ -821,8 +822,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
Expand All @@ -835,16 +835,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
fmt.Sprintf("Waiting for std compute nodes completion finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
}
}

// Save the state post wait completion:
err = populateRosaHcpClusterState(ctx, object, state)
err = populateRosaHcpClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -854,9 +853,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
)
return
}

diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
}

func (r *ClusterRosaHcpResource) Read(ctx context.Context, request resource.ReadRequest,
Expand Down

0 comments on commit 011f585

Please sign in to comment.