@@ -165,6 +165,17 @@ func (r *customPolicyResource) getContent(data customPolicyResourceModel) ([]byt
165165 return policyBundleData , newCrc32Checksum (policyBundleData ), nil
166166}
167167
168+ func (r * customPolicyResource ) getScope (data * customPolicyResourceModel ) string {
169+ scopeMrn := "//platform.api.mondoo.app"
170+ if ! data .ScopeMrn .IsNull () {
171+ scopeMrn = data .ScopeMrn .ValueString ()
172+ } else if space , err := r .client .ComputeSpace (data .SpaceID ); err == nil {
173+ // Compute and validate the space
174+ scopeMrn = space .MRN ()
175+ }
176+ return scopeMrn
177+ }
178+
168179func (r * customPolicyResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
169180 var data customPolicyResourceModel
170181
@@ -175,14 +186,7 @@ func (r *customPolicyResource) Create(ctx context.Context, req resource.CreateRe
175186 return
176187 }
177188
178- scopeMrn := "//policy.api.mondoo.app"
179- if ! data .ScopeMrn .IsNull () {
180- scopeMrn = data .ScopeMrn .ValueString ()
181- } else if space , err := r .client .ComputeSpace (data .SpaceID ); err == nil {
182- // Compute and validate the space
183- scopeMrn = space .MRN ()
184- }
185-
189+ scopeMrn := r .getScope (& data )
186190 ctx = tflog .SetField (ctx , "scope_mrn" , scopeMrn )
187191
188192 // Do GraphQL request to API to create the resource
@@ -265,16 +269,8 @@ func (r *customPolicyResource) Update(ctx context.Context, req resource.UpdateRe
265269 return
266270 }
267271
268- var scopeMrn string
269- // Compute and validate the space
270- space , err := r .client .ComputeSpace (data .SpaceID )
271- if err != nil {
272- resp .Diagnostics .AddError ("Invalid Configuration" , err .Error ())
273- return
274- } else {
275- scopeMrn = space .MRN ()
276- }
277- ctx = tflog .SetField (ctx , "space_mrn" , space .MRN ())
272+ scopeMrn := r .getScope (& data )
273+ ctx = tflog .SetField (ctx , "scope_mrn" , scopeMrn )
278274
279275 // check if the local content has changed, if so, update the policy
280276 policyBundleData , checksum , err := r .getContent (data )
0 commit comments