1
1
package blueprint_permissions
2
2
3
3
import (
4
+ "github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
4
5
"strings"
5
6
6
7
"github.com/hashicorp/terraform-plugin-framework/types"
7
8
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
8
9
)
9
10
10
- func goStringListToTFList (list []string ) []types.String {
11
- var result = make ([]types.String , len (list ))
12
- for i , u := range list {
13
- result [i ] = types .StringValue (u )
14
- }
15
-
16
- return result
17
- }
18
-
19
- func blueprintPermissionsBlockToBlueprintPermissionsTFBlock (block cli.BlueprintPermissionsBlock ) * BlueprintPermissionsTFBlock {
20
- return & BlueprintPermissionsTFBlock {
21
- Users : goStringListToTFList (block .Users ),
22
- Roles : goStringListToTFList (block .Roles ),
23
- Teams : goStringListToTFList (block .Teams ),
24
- OwnedByTeam : types .BoolValue (* block .OwnedByTeam ),
11
+ func refreshBlueprintPermissionsState (state * BlueprintPermissionsModel , a * cli.BlueprintPermissions , blueprintId string ) error {
12
+ oldPermissions := state .Entities
13
+ if oldPermissions == nil {
14
+ oldPermissions = & EntitiesBlueprintPermissionsModel {}
25
15
}
26
- }
27
16
28
- func refreshBlueprintPermissionsState (state * BlueprintPermissionsModel , a * cli.BlueprintPermissions , blueprintId string ) error {
29
17
state .ID = types .StringValue (blueprintId )
30
18
state .BlueprintIdentifier = types .StringValue (blueprintId )
31
19
state .Entities = & EntitiesBlueprintPermissionsModel {}
32
- state .Entities .Update = blueprintPermissionsBlockToBlueprintPermissionsTFBlock (a .Entities .Update )
33
- state .Entities .Unregister = blueprintPermissionsBlockToBlueprintPermissionsTFBlock (a .Entities .Unregister )
34
- state .Entities .Register = blueprintPermissionsBlockToBlueprintPermissionsTFBlock (a .Entities .Register )
20
+
21
+ if oldPermissions .Update == nil {
22
+ state .Entities .Update = & BlueprintPermissionsTFBlock {
23
+ Users : utils .Map (a .Entities .Update .Users , types .StringValue ),
24
+ Roles : utils .Map (a .Entities .Update .Roles , types .StringValue ),
25
+ Teams : utils .Map (a .Entities .Update .Teams , types .StringValue ),
26
+ OwnedByTeam : types .BoolValue (* a .Entities .Update .OwnedByTeam ),
27
+ }
28
+ } else {
29
+ state .Entities .Update = & BlueprintPermissionsTFBlock {
30
+ Users : utils .Map (utils .SortStringSliceByOther (a .Entities .Update .Users , utils .TFStringListToStringArray (oldPermissions .Update .Users )), types .StringValue ),
31
+ Roles : utils .Map (utils .SortStringSliceByOther (a .Entities .Update .Roles , utils .TFStringListToStringArray (oldPermissions .Update .Roles )), types .StringValue ),
32
+ Teams : utils .Map (utils .SortStringSliceByOther (a .Entities .Update .Teams , utils .TFStringListToStringArray (oldPermissions .Update .Teams )), types .StringValue ),
33
+ OwnedByTeam : types .BoolValue (* a .Entities .Update .OwnedByTeam ),
34
+ }
35
+ }
36
+
37
+ if oldPermissions .Unregister == nil {
38
+ state .Entities .Unregister = & BlueprintPermissionsTFBlock {
39
+ Users : utils .Map (a .Entities .Unregister .Users , types .StringValue ),
40
+ Roles : utils .Map (a .Entities .Unregister .Roles , types .StringValue ),
41
+ Teams : utils .Map (a .Entities .Unregister .Teams , types .StringValue ),
42
+ OwnedByTeam : types .BoolValue (* a .Entities .Unregister .OwnedByTeam ),
43
+ }
44
+ } else {
45
+ state .Entities .Unregister = & BlueprintPermissionsTFBlock {
46
+ Users : utils .Map (utils .SortStringSliceByOther (a .Entities .Unregister .Users , utils .TFStringListToStringArray (oldPermissions .Unregister .Users )), types .StringValue ),
47
+ Roles : utils .Map (utils .SortStringSliceByOther (a .Entities .Unregister .Roles , utils .TFStringListToStringArray (oldPermissions .Unregister .Roles )), types .StringValue ),
48
+ Teams : utils .Map (utils .SortStringSliceByOther (a .Entities .Unregister .Teams , utils .TFStringListToStringArray (oldPermissions .Unregister .Teams )), types .StringValue ),
49
+ OwnedByTeam : types .BoolValue (* a .Entities .Unregister .OwnedByTeam ),
50
+ }
51
+ }
52
+
53
+ if oldPermissions .Register == nil {
54
+ state .Entities .Register = & BlueprintPermissionsTFBlock {
55
+ Users : utils .Map (a .Entities .Register .Users , types .StringValue ),
56
+ Roles : utils .Map (a .Entities .Register .Roles , types .StringValue ),
57
+ Teams : utils .Map (a .Entities .Register .Teams , types .StringValue ),
58
+ OwnedByTeam : types .BoolValue (* a .Entities .Register .OwnedByTeam ),
59
+ }
60
+ } else {
61
+ state .Entities .Register = & BlueprintPermissionsTFBlock {
62
+ Users : utils .Map (utils .SortStringSliceByOther (a .Entities .Register .Users , utils .TFStringListToStringArray (oldPermissions .Register .Users )), types .StringValue ),
63
+ Roles : utils .Map (utils .SortStringSliceByOther (a .Entities .Register .Roles , utils .TFStringListToStringArray (oldPermissions .Register .Roles )), types .StringValue ),
64
+ Teams : utils .Map (utils .SortStringSliceByOther (a .Entities .Register .Teams , utils .TFStringListToStringArray (oldPermissions .Register .Teams )), types .StringValue ),
65
+ OwnedByTeam : types .BoolValue (* a .Entities .Register .OwnedByTeam ),
66
+ }
67
+ }
68
+
69
+ if oldPermissions .UpdateProperties == nil {
70
+ oldPermissions .UpdateProperties = & BlueprintRelationsPermissionsTFBlock {}
71
+ }
35
72
36
73
state .Entities .UpdateProperties = nil
37
74
var mappedUpdateProperties BlueprintRelationsPermissionsTFBlock = nil
38
75
if len (a .Entities .UpdateProperties ) > 0 {
39
76
state .Entities .UpdateMetadataProperties = & BlueprintMetadataPermissionsTFBlock {}
40
77
mappedUpdateProperties = make (BlueprintRelationsPermissionsTFBlock )
41
78
for updatePropertyKey , updatePropertyValue := range a .Entities .UpdateProperties {
42
- var current = blueprintPermissionsBlockToBlueprintPermissionsTFBlock (updatePropertyValue )
79
+ var oldPropValue * BlueprintPermissionsTFBlock
80
+ if strings .HasPrefix (updatePropertyKey , "$" ) {
81
+ switch updatePropertyKey {
82
+ case "$title" :
83
+ oldPropValue = oldPermissions .UpdateMetadataProperties .Title
84
+ case "$identifier" :
85
+ oldPropValue = oldPermissions .UpdateMetadataProperties .Identifier
86
+ case "$icon" :
87
+ oldPropValue = oldPermissions .UpdateMetadataProperties .Icon
88
+ case "$team" :
89
+ oldPropValue = oldPermissions .UpdateMetadataProperties .Team
90
+ }
91
+ } else if val , ok := (* oldPermissions .UpdateProperties )[updatePropertyKey ]; ok {
92
+ oldPropValue = & val
93
+ }
94
+ var current * BlueprintPermissionsTFBlock
95
+ if oldPropValue == nil {
96
+ current = & BlueprintPermissionsTFBlock {
97
+ Users : utils .Map (updatePropertyValue .Users , types .StringValue ),
98
+ Roles : utils .Map (updatePropertyValue .Roles , types .StringValue ),
99
+ Teams : utils .Map (updatePropertyValue .Teams , types .StringValue ),
100
+ OwnedByTeam : types .BoolValue (* updatePropertyValue .OwnedByTeam ),
101
+ }
102
+ } else {
103
+ current = & BlueprintPermissionsTFBlock {
104
+ Users : utils .Map (utils .SortStringSliceByOther (updatePropertyValue .Users , utils .TFStringListToStringArray (oldPropValue .Users )), types .StringValue ),
105
+ Roles : utils .Map (utils .SortStringSliceByOther (updatePropertyValue .Roles , utils .TFStringListToStringArray (oldPropValue .Roles )), types .StringValue ),
106
+ Teams : utils .Map (utils .SortStringSliceByOther (updatePropertyValue .Teams , utils .TFStringListToStringArray (oldPropValue .Teams )), types .StringValue ),
107
+ OwnedByTeam : types .BoolValue (* updatePropertyValue .OwnedByTeam ),
108
+ }
109
+ }
43
110
44
111
if strings .HasPrefix (updatePropertyKey , "$" ) {
45
112
switch updatePropertyKey {
@@ -60,10 +127,26 @@ func refreshBlueprintPermissionsState(state *BlueprintPermissionsModel, a *cli.B
60
127
state .Entities .UpdateProperties = & mappedUpdateProperties
61
128
}
62
129
}
130
+
63
131
if len (a .Entities .UpdateRelations ) > 0 {
64
132
var mappedUpdateRelations = make (BlueprintRelationsPermissionsTFBlock , len (a .Entities .UpdateRelations ))
65
133
for updateRelationKey , updateRelationValue := range a .Entities .UpdateRelations {
66
- mappedUpdateRelations [updateRelationKey ] = * blueprintPermissionsBlockToBlueprintPermissionsTFBlock (updateRelationValue )
134
+ oldRelValue , hasOldRelValue := (* oldPermissions .UpdateRelations )[updateRelationKey ]
135
+ if ! hasOldRelValue {
136
+ mappedUpdateRelations [updateRelationKey ] = BlueprintPermissionsTFBlock {
137
+ Users : utils .Map (updateRelationValue .Users , types .StringValue ),
138
+ Roles : utils .Map (updateRelationValue .Roles , types .StringValue ),
139
+ Teams : utils .Map (updateRelationValue .Teams , types .StringValue ),
140
+ OwnedByTeam : types .BoolValue (* updateRelationValue .OwnedByTeam ),
141
+ }
142
+ } else {
143
+ mappedUpdateRelations [updateRelationKey ] = BlueprintPermissionsTFBlock {
144
+ Users : utils .Map (utils .SortStringSliceByOther (updateRelationValue .Users , utils .TFStringListToStringArray (oldRelValue .Users )), types .StringValue ),
145
+ Roles : utils .Map (utils .SortStringSliceByOther (updateRelationValue .Roles , utils .TFStringListToStringArray (oldRelValue .Roles )), types .StringValue ),
146
+ Teams : utils .Map (utils .SortStringSliceByOther (updateRelationValue .Teams , utils .TFStringListToStringArray (oldRelValue .Teams )), types .StringValue ),
147
+ OwnedByTeam : types .BoolValue (* updateRelationValue .OwnedByTeam ),
148
+ }
149
+ }
67
150
}
68
151
state .Entities .UpdateRelations = & mappedUpdateRelations
69
152
} else {
0 commit comments