99 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
1010 apierrors "k8s.io/apimachinery/pkg/api/errors"
1111 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12- "k8s.io/utils/pointer"
1312 "strings"
1413 "time"
1514)
@@ -91,10 +90,61 @@ func resourceRoleBinding() *schema.Resource {
9190 Type : schema .TypeString ,
9291 },
9392 },
94- "cel" : {
95- Type : schema .TypeString ,
96- Optional : true ,
97- Description : descriptions ["rolebinding_cel" ],
93+ "condition_resource_names" : {
94+ ConflictsWith : []string {"condition_cel" },
95+ Type : schema .TypeList ,
96+ Optional : true ,
97+ Description : descriptions ["rolebinding_condition_resource_names" ],
98+ Elem : & schema.Resource {
99+ Schema : map [string ]* schema.Schema {
100+ "organization" : {
101+ Type : schema .TypeString ,
102+ Optional : true ,
103+ Description : descriptions ["rolebinding_condition_resource_names_organization" ],
104+ },
105+ "instance" : {
106+ Type : schema .TypeString ,
107+ Optional : true ,
108+ Description : descriptions ["rolebinding_condition_resource_names_instance" ],
109+ },
110+ "cluster" : {
111+ Type : schema .TypeString ,
112+ Optional : true ,
113+ Description : descriptions ["rolebinding_condition_resource_names_cluster" ],
114+ },
115+ "tenant" : {
116+ Type : schema .TypeString ,
117+ Optional : true ,
118+ Description : descriptions ["rolebinding_condition_resource_names_tenant" ],
119+ },
120+ "namespace" : {
121+ Type : schema .TypeString ,
122+ Optional : true ,
123+ Description : descriptions ["rolebinding_condition_resource_names_namespace" ],
124+ },
125+ "topic_domain" : {
126+ Type : schema .TypeString ,
127+ Optional : true ,
128+ Description : descriptions ["rolebinding_condition_resource_names_topic_domain" ],
129+ },
130+ "topic_name" : {
131+ Type : schema .TypeString ,
132+ Optional : true ,
133+ Description : descriptions ["rolebinding_condition_resource_names_topic_name" ],
134+ },
135+ "subscription" : {
136+ Type : schema .TypeString ,
137+ Optional : true ,
138+ Description : descriptions ["rolebinding_condition_resource_names_subscription" ],
139+ },
140+ },
141+ },
142+ },
143+ "condition_cel" : {
144+ Type : schema .TypeString ,
145+ Optional : true ,
146+ Description : descriptions ["rolebinding_condition_cel" ],
147+ ConflictsWith : []string {"condition_resource_names" },
98148 },
99149 },
100150 }
@@ -107,7 +157,6 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in
107157 predefinedRoleName := d .Get ("cluster_role_name" ).(string )
108158 serviceAccountNames := d .Get ("service_account_names" ).([]interface {})
109159 userNames := d .Get ("user_names" ).([]interface {})
110- cel := d .Get ("cel" ).(string )
111160
112161 clientSet , err := getClientSet (getFactoryFromMeta (m ))
113162 if err != nil {
@@ -154,9 +203,7 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in
154203 }
155204 }
156205
157- if cel != "" {
158- rb .Spec .CEL = pointer .String (cel )
159- }
206+ conditionSet (namespace , d , rb )
160207
161208 if _ , err := clientSet .CloudV1alpha1 ().RoleBindings (namespace ).Create (ctx , rb , metav1.CreateOptions {
162209 FieldManager : "terraform-create" ,
@@ -204,6 +251,7 @@ func resourceRoleBindingDelete(ctx context.Context, d *schema.ResourceData, m in
204251func resourceRoleBindingUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
205252 namespace := d .Get ("organization" ).(string )
206253 name := d .Get ("name" ).(string )
254+ userNames := d .Get ("user_names" ).([]interface {})
207255 clientSet , err := getClientSet (getFactoryFromMeta (m ))
208256 if err != nil {
209257 return diag .FromErr (fmt .Errorf ("ERROR_INIT_CLIENT_ON_READ_ROLEBINDING: %w" , err ))
@@ -215,8 +263,9 @@ func resourceRoleBindingUpdate(ctx context.Context, d *schema.ResourceData, m in
215263
216264 serviceAccountNames := d .Get ("service_account_names" ).([]interface {})
217265
266+ roleBinding .Spec .Subjects = []v1alpha1.Subject {}
267+
218268 if serviceAccountNames != nil {
219- roleBinding .Spec .Subjects = []v1alpha1.Subject {}
220269 for _ , serviceAccountName := range serviceAccountNames {
221270 roleBinding .Spec .Subjects = append (roleBinding .Spec .Subjects , v1alpha1.Subject {
222271 APIGroup : "cloud.streamnative.io" ,
@@ -225,6 +274,17 @@ func resourceRoleBindingUpdate(ctx context.Context, d *schema.ResourceData, m in
225274 })
226275 }
227276 }
277+ if userNames != nil {
278+ for _ , userName := range userNames {
279+ roleBinding .Spec .Subjects = append (roleBinding .Spec .Subjects , v1alpha1.Subject {
280+ APIGroup : "cloud.streamnative.io" ,
281+ Name : userName .(string ),
282+ Kind : "User" ,
283+ })
284+ }
285+ }
286+
287+ conditionSet (namespace , d , roleBinding )
228288 _ , err = clientSet .CloudV1alpha1 ().RoleBindings (namespace ).Update (ctx , roleBinding , metav1.UpdateOptions {})
229289 if err != nil {
230290 return diag .FromErr (fmt .Errorf ("ERROR_UPDATE_ROLEBINDING: %w" , err ))
@@ -284,3 +344,32 @@ func resourceRoleBindingRead(ctx context.Context, d *schema.ResourceData, m inte
284344 d .SetId (fmt .Sprintf ("%s/%s" , roleBinding .Namespace , roleBinding .Name ))
285345 return nil
286346}
347+
348+ func conditionSet (organization string , d * schema.ResourceData , binding * v1alpha1.RoleBinding ) {
349+ cel , exist := d .GetOk ("condition_cel" )
350+ if exist {
351+ celExpression := cel .(string )
352+ binding .Spec .CEL = & celExpression
353+ }
354+
355+ resourceNames := d .Get ("condition_resource_names" )
356+ if resourceNames != nil {
357+ var bindingResourceNames []v1alpha1.ResourceName
358+ resourceNamesEntity := resourceNames .([]interface {})
359+ for idx := range resourceNamesEntity {
360+ resourceName := resourceNamesEntity [idx ]
361+ resourceElements := resourceName .(map [string ]interface {})
362+ bindingResourceNames = append (bindingResourceNames , v1alpha1.ResourceName {
363+ Organization : organization ,
364+ Instance : resourceElements ["instance" ].(string ),
365+ Cluster : resourceElements ["cluster" ].(string ),
366+ Tenant : resourceElements ["tenant" ].(string ),
367+ Namespace : resourceElements ["namespace" ].(string ),
368+ TopicDomain : resourceElements ["topic_domain" ].(string ),
369+ TopicName : resourceElements ["topic_name" ].(string ),
370+ Subscription : resourceElements ["subscription" ].(string ),
371+ })
372+ }
373+ binding .Spec .ResourceNames = bindingResourceNames
374+ }
375+ }
0 commit comments