Skip to content

Commit 1b3c7e5

Browse files
committed
chore: refactor name and parameter for classic/hcp
1 parent 18b4511 commit 1b3c7e5

File tree

6 files changed

+54
-59
lines changed

6 files changed

+54
-59
lines changed

provider/clusterrosa/classic/cluster_rosa_classic_datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (r *ClusterRosaClassicDatasource) Read(ctx context.Context, request datasou
378378
object := get.Body()
379379

380380
// Save the state:
381-
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
381+
err = populateRosaClassicClusterState(ctx, object, state)
382382
if err != nil {
383383
response.Diagnostics.AddError(
384384
"Can't populate cluster state",

provider/clusterrosa/classic/cluster_rosa_classic_resource.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,8 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
792792
tflog.Debug(ctx, "begin create()")
793793

794794
// Get the plan:
795-
state := &ClusterRosaClassicState{}
796-
diags := request.Plan.Get(ctx, state)
795+
plan := &ClusterRosaClassicState{}
796+
diags := request.Plan.Get(ctx, plan)
797797
response.Diagnostics.Append(diags...)
798798
if response.Diagnostics.HasError() {
799799
return
@@ -802,7 +802,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
802802

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

813813
channelGroup := consts.DefaultChannelGroup
814-
if common.HasValue(state.ChannelGroup) {
815-
channelGroup = state.ChannelGroup.ValueString()
814+
if common.HasValue(plan.ChannelGroup) {
815+
channelGroup = plan.ChannelGroup.ValueString()
816816
}
817817
desiredVersion := ""
818-
if common.HasValue(state.Version) {
819-
desiredVersion = state.Version.ValueString()
818+
if common.HasValue(plan.Version) {
819+
desiredVersion = plan.Version.ValueString()
820820
}
821821
version, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Classic, channelGroup, desiredVersion)
822822
if err != nil {
823823
response.Diagnostics.AddError(
824824
summary,
825825
fmt.Sprintf(
826826
"Can't build cluster with name '%s': %v",
827-
state.Name.ValueString(), err,
827+
plan.Name.ValueString(), err,
828828
),
829829
)
830830
return
831831
}
832832

833-
err = validateHttpTokensVersion(ctx, state, version)
833+
err = validateHttpTokensVersion(ctx, plan, version)
834834
if err != nil {
835835
response.Diagnostics.AddError(
836836
summary,
837837
fmt.Sprintf(
838838
"Can't build cluster with name '%s': %v",
839-
state.Name.ValueString(), err,
839+
plan.Name.ValueString(), err,
840840
),
841841
)
842842
return
843843
}
844844

845-
object, err := createClassicClusterObject(ctx, state, diags)
845+
object, err := createClassicClusterObject(ctx, plan, diags)
846846
if err != nil {
847847
response.Diagnostics.AddError(
848848
summary,
849849
fmt.Sprintf(
850850
"Can't build cluster with name '%s': %v",
851-
state.Name.ValueString(), err,
851+
plan.Name.ValueString(), err,
852852
),
853853
)
854854
return
@@ -860,15 +860,15 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
860860
summary,
861861
fmt.Sprintf(
862862
"Can't create cluster with name '%s': %v",
863-
state.Name.ValueString(), err,
863+
plan.Name.ValueString(), err,
864864
),
865865
)
866866
return
867867
}
868868
object = add.Body()
869869

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

882-
if common.HasValue(state.WaitForCreateComplete) && state.WaitForCreateComplete.ValueBool() {
883+
if common.HasValue(plan.WaitForCreateComplete) && plan.WaitForCreateComplete.ValueBool() {
883884
object, err = r.ClusterWait.WaitForClusterToBeReady(ctx, object.ID(), rosa.DefaultWaitTimeoutInMinutes)
884885
if err != nil {
885886
response.Diagnostics.AddError(
886887
"Waiting for cluster creation finished with error",
887888
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
888889
)
889890
if object == nil {
890-
diags = response.State.Set(ctx, state)
891-
response.Diagnostics.Append(diags...)
891+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
892892
return
893893
}
894894
}
895895
}
896896

897897
// Save the state post wait completion:
898-
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
898+
err = populateRosaClassicClusterState(ctx, object, plan)
899899
if err != nil {
900900
response.Diagnostics.AddError(
901901
"Can't populate cluster state",
@@ -905,9 +905,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
905905
)
906906
return
907907
}
908-
909-
diags = response.State.Set(ctx, state)
910-
response.Diagnostics.Append(diags...)
908+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
911909
}
912910

913911
func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.ReadRequest,
@@ -944,7 +942,7 @@ func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.
944942
object := get.Body()
945943

946944
// Save the state:
947-
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
945+
err = populateRosaClassicClusterState(ctx, object, state)
948946
if err != nil {
949947
response.Diagnostics.AddError(
950948
"Can't populate cluster state",
@@ -1137,7 +1135,7 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request resourc
11371135
object := update.Body()
11381136

11391137
// Update the state:
1140-
err = populateRosaClassicClusterState(ctx, object, plan, common.DefaultHttpClient{})
1138+
err = populateRosaClassicClusterState(ctx, object, plan)
11411139
if err != nil {
11421140
response.Diagnostics.AddError(
11431141
"Can't populate cluster state",
@@ -1409,7 +1407,7 @@ func (r *ClusterRosaClassicResource) ImportState(ctx context.Context, request re
14091407
}
14101408

14111409
// populateRosaClassicClusterState copies the data from the API object to the Terraform state.
1412-
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState, httpClient common.HttpClient) error {
1410+
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState) error {
14131411
state.ID = types.StringValue(object.ID())
14141412
state.ExternalID = types.StringValue(object.ExternalID())
14151413
object.API()

provider/clusterrosa/classic/cluster_rosa_classic_resource_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
237237

238238
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
239239
Expect(err).ToNot(HaveOccurred())
240-
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)).To(Succeed())
240+
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)).To(Succeed())
241241

242242
Expect(clusterState.ID.ValueString()).To(Equal(clusterId))
243243
Expect(clusterState.CloudRegion.ValueString()).To(Equal(regionId))
@@ -282,7 +282,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
282282
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
283283
Expect(err).ToNot(HaveOccurred())
284284

285-
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
285+
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
286286
Expect(err).ToNot(HaveOccurred())
287287
Expect(clusterState.Sts.OIDCEndpointURL.ValueString()).To(Equal("nonce.com"))
288288
})
@@ -298,7 +298,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
298298
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
299299
Expect(err).ToNot(HaveOccurred())
300300

301-
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
301+
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
302302
Expect(err).ToNot(HaveOccurred())
303303
Expect(clusterState.Sts.Thumbprint.ValueString()).To(Equal(""))
304304
})

provider/clusterrosa/hcp/datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (r *ClusterRosaHcpDatasource) Read(ctx context.Context, request datasource.
302302
object := get.Body()
303303

304304
// Save the state:
305-
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
305+
err = populateRosaHcpClusterState(ctx, object, state)
306306
if err != nil {
307307
response.Diagnostics.AddError(
308308
"Can't populate cluster state",

provider/clusterrosa/hcp/resource.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -624,16 +624,16 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
624624
tflog.Debug(ctx, "begin create()")
625625

626626
// Get the plan:
627-
state := &ClusterRosaHcpState{}
628-
diags := request.Plan.Get(ctx, state)
627+
plan := &ClusterRosaHcpState{}
628+
diags := request.Plan.Get(ctx, plan)
629629
response.Diagnostics.Append(diags...)
630630
if response.Diagnostics.HasError() {
631631
return
632632
}
633633
summary := "Can't build cluster"
634634

635-
shouldWaitCreationComplete := common.BoolWithFalseDefault(state.WaitForCreateComplete)
636-
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(state.WaitForStdComputeNodesComplete)
635+
shouldWaitCreationComplete := common.BoolWithFalseDefault(plan.WaitForCreateComplete)
636+
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(plan.WaitForStdComputeNodesComplete)
637637
if shouldWaitComputeNodesComplete && !shouldWaitCreationComplete {
638638
response.Diagnostics.AddError(
639639
summary,
@@ -642,8 +642,8 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
642642
return
643643
}
644644

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

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

665665
channelGroup := consts.DefaultChannelGroup
666-
if common.HasValue(state.ChannelGroup) {
667-
channelGroup = state.ChannelGroup.ValueString()
666+
if common.HasValue(plan.ChannelGroup) {
667+
channelGroup = plan.ChannelGroup.ValueString()
668668
}
669669
desiredVersion := ""
670-
if common.HasValue(state.Version) {
671-
desiredVersion = state.Version.ValueString()
670+
if common.HasValue(plan.Version) {
671+
desiredVersion = plan.Version.ValueString()
672672
}
673673
_, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Hcp, channelGroup, desiredVersion)
674674
if err != nil {
675675
response.Diagnostics.AddError(
676676
summary,
677677
fmt.Sprintf(
678678
"Can't build cluster with name '%s': %v",
679-
state.Name.ValueString(), err,
679+
plan.Name.ValueString(), err,
680680
),
681681
)
682682
return
683683
}
684684

685-
object, err := createHcpClusterObject(ctx, state, diags)
685+
object, err := createHcpClusterObject(ctx, plan, diags)
686686
if err != nil {
687687
response.Diagnostics.AddError(
688688
summary,
689689
fmt.Sprintf(
690690
"Can't build cluster with name '%s': %v",
691-
state.Name.ValueString(), err,
691+
plan.Name.ValueString(), err,
692692
),
693693
)
694694
return
@@ -700,15 +700,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
700700
summary,
701701
fmt.Sprintf(
702702
"Can't create cluster with name '%s': %v",
703-
state.Name.ValueString(), err,
703+
plan.Name.ValueString(), err,
704704
),
705705
)
706706
return
707707
}
708708
object = add.Body()
709709

710710
// Save initial state:
711-
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
711+
err = populateRosaHcpClusterState(ctx, object, plan)
712712
if err != nil {
713713
response.Diagnostics.AddError(
714714
"Can't populate cluster state",
@@ -718,6 +718,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
718718
)
719719
return
720720
}
721+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
721722

722723
if shouldWaitCreationComplete {
723724
tflog.Info(ctx, "Waiting for cluster to get ready")
@@ -728,8 +729,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
728729
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
729730
)
730731
if object == nil {
731-
diags = response.State.Set(ctx, state)
732-
response.Diagnostics.Append(diags...)
732+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
733733
return
734734
}
735735
}
@@ -742,16 +742,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
742742
fmt.Sprintf("Waiting for std compute nodes completion finished with the error %v", err),
743743
)
744744
if object == nil {
745-
diags = response.State.Set(ctx, state)
746-
response.Diagnostics.Append(diags...)
745+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
747746
return
748747
}
749748
}
750749
}
751750
}
752751

753752
// Save the state post wait completion:
754-
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
753+
err = populateRosaHcpClusterState(ctx, object, plan)
755754
if err != nil {
756755
response.Diagnostics.AddError(
757756
"Can't populate cluster state",
@@ -761,9 +760,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
761760
)
762761
return
763762
}
764-
765-
diags = response.State.Set(ctx, state)
766-
response.Diagnostics.Append(diags...)
763+
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
767764
}
768765

769766
func (r *ClusterRosaHcpResource) Read(ctx context.Context, request resource.ReadRequest,
@@ -800,7 +797,7 @@ func (r *ClusterRosaHcpResource) Read(ctx context.Context, request resource.Read
800797
object := get.Body()
801798

802799
// Save the state:
803-
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
800+
err = populateRosaHcpClusterState(ctx, object, state)
804801
if err != nil {
805802
response.Diagnostics.AddError(
806803
"Can't populate cluster state",
@@ -978,7 +975,7 @@ func (r *ClusterRosaHcpResource) Update(ctx context.Context, request resource.Up
978975
object := update.Body()
979976

980977
// Update the state:
981-
err = populateRosaHcpClusterState(ctx, object, plan, common.DefaultHttpClient{})
978+
err = populateRosaHcpClusterState(ctx, object, plan)
982979
if err != nil {
983980
response.Diagnostics.AddError(
984981
"Can't populate cluster state",
@@ -1242,7 +1239,7 @@ func (r *ClusterRosaHcpResource) ImportState(ctx context.Context, request resour
12421239
}
12431240

12441241
// populateRosaHcpClusterState copies the data from the API object to the Terraform state.
1245-
func populateRosaHcpClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaHcpState, httpClient common.HttpClient) error {
1242+
func populateRosaHcpClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaHcpState) error {
12461243
state.ID = types.StringValue(object.ID())
12471244
state.ExternalID = types.StringValue(object.ExternalID())
12481245
object.API()

0 commit comments

Comments
 (0)