Skip to content

Commit

Permalink
fix: standardised use of object throughout crud functions (#259)
Browse files Browse the repository at this point in the history
* fix: update resource configurations to use assignment syntax for settings

* refactor: rename variable 'plan' to 'object' for consistency in CRUD operations

* refactor: rename variable 'plan' to 'object' for consistency in CRUD operations

* chore: update go documentation, provider documentation and format terraform files

Signed-off-by: GitHub Actions Bot <github-actions[bot]@users.noreply.github.com>

---------

Signed-off-by: GitHub Actions Bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: GitHub Actions Bot <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ShocOne and github-actions[bot] authored Dec 17, 2024
1 parent a385fce commit bbc3dc3
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resource "microsoft365_graph_device_and_app_management_cloud_pc_user_setting" "e
local_admin_enabled = true
reset_enabled = false

restore_point_setting {
restore_point_setting = {
frequency_type = "sixHours"
user_restore_enabled = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,57 @@ resource "microsoft365_graph_beta_identity_and_access_conditional_access_policy"
display_name = "Example Conditional Access Policy"
state = "enabled"

conditions {
applications {
conditions = {
applications = {
include_applications = ["All"]
exclude_applications = ["MicrosoftAdminPortals"]
application_filter {
application_filter = {
mode = "include"
rule = "(appId -eq '11111111-1111-1111-1111-111111111111')"
}
include_user_actions = ["urn:user:registersecurityinfo"]
}

authentication_flows {
authentication_flows = {
transfer_methods = "deviceCodeFlow"
}

users {
users = {
include_users = ["All"]
exclude_users = ["11111111-1111-1111-1111-111111111111"]
include_groups = ["22222222-2222-2222-2222-222222222222"]
}

client_applications {
client_applications = {
include_service_principals = ["ServicePrincipalsInMyTenant"]
exclude_service_principals = ["33333333-3333-3333-3333-333333333333"]
service_principal_filter {
service_principal_filter = {
mode = "include"
rule = "(servicePrincipalId -eq '44444444-4444-4444-4444-444444444444')"
}
}

client_app_types = ["all"]

locations {
locations = {
include_locations = ["All"]
exclude_locations = ["55555555-5555-5555-5555-555555555555"]
}

platforms {
platforms = {
include_platforms = ["android", "iOS"]
exclude_platforms = ["windows"]
}

device_states {
device_states = {
include_states = ["All"]
exclude_states = ["Compliant"]
}

devices {
devices = {
include_devices = ["All"]
exclude_devices = ["DomainJoined"]
device_filter {
device_filter = {
mode = "exclude"
rule = "(device.deviceId -eq '66666666-6666-6666-6666-666666666666')"
}
Expand All @@ -62,13 +62,13 @@ resource "microsoft365_graph_beta_identity_and_access_conditional_access_policy"
user_risk_levels = ["low", "medium"]
}

grant_controls {
grant_controls = {
operator = "OR"
built_in_controls = ["mfa", "compliantDevice"]
custom_authentication_factors = ["77777777-7777-7777-7777-777777777777"]
terms_of_use = ["88888888-8888-8888-8888-888888888888"]

authentication_strength {
authentication_strength = {
id = "99999999-9999-9999-9999-999999999999"
display_name = "Example Authentication Strength"
description = "A description for the authentication strength."
Expand All @@ -78,26 +78,26 @@ resource "microsoft365_graph_beta_identity_and_access_conditional_access_policy"
}
}

session_controls {
application_enforced_restrictions {
session_controls = {
application_enforced_restrictions = {
is_enabled = true
}

cloud_app_security {
cloud_app_security = {
is_enabled = true
cloud_app_security_type = "monitorOnly"
}

continuous_access_evaluation {
continuous_access_evaluation = {
mode = "strictEnforcement"
}

persistent_browser {
persistent_browser = {
is_enabled = true
mode = "always"
}

sign_in_frequency {
sign_in_frequency = {
is_enabled = true
type = "days"
value = 1
Expand All @@ -107,7 +107,7 @@ resource "microsoft365_graph_beta_identity_and_access_conditional_access_policy"

disable_resilience_defaults = false

secure_sign_in_session {
secure_sign_in_session = {
is_enabled = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import (

// Create handles the Create operation.
func (r *BrowserSiteResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan BrowserSiteResourceModel
var object BrowserSiteResourceModel

tflog.Debug(ctx, fmt.Sprintf("Starting creation of resource: %s_%s", r.ProviderTypeName, r.TypeName))

resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.Plan.Get(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}

ctx, cancel := crud.HandleTimeout(ctx, plan.Timeouts.Create, CreateTimeout*time.Second, &resp.Diagnostics)
ctx, cancel := crud.HandleTimeout(ctx, object.Timeouts.Create, CreateTimeout*time.Second, &resp.Diagnostics)
if cancel == nil {
return
}
defer cancel()

requestBody, err := constructResource(ctx, &plan)
requestBody, err := constructResource(ctx, &object)
if err != nil {
resp.Diagnostics.AddError(
"Error constructing browser site",
Expand All @@ -38,7 +38,7 @@ func (r *BrowserSiteResource) Create(ctx context.Context, req resource.CreateReq
return
}

browserSiteListId := plan.BrowserSiteListAssignmentID.ValueString()
browserSiteListId := object.BrowserSiteListAssignmentID.ValueString()

createdSite, err := r.client.
Admin().
Expand All @@ -53,17 +53,16 @@ func (r *BrowserSiteResource) Create(ctx context.Context, req resource.CreateReq
return
}

plan.ID = types.StringValue(*createdSite.GetId())
object.ID = types.StringValue(*createdSite.GetId())
if resp.Diagnostics.HasError() {
return
}

MapRemoteStateToTerraform(ctx, &plan, createdSite)
MapRemoteStateToTerraform(ctx, &object, createdSite)

// Explicitly set BrowserSiteListAssignmentID in the state as it's not in the resp.
plan.BrowserSiteListAssignmentID = types.StringValue(browserSiteListId)
object.BrowserSiteListAssignmentID = types.StringValue(browserSiteListId)

resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -73,24 +72,25 @@ func (r *BrowserSiteResource) Create(ctx context.Context, req resource.CreateReq

// Read handles the Read operation.
func (r *BrowserSiteResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state BrowserSiteResourceModel
var object BrowserSiteResourceModel

tflog.Debug(ctx, fmt.Sprintf("Starting Read method for: %s_%s", r.ProviderTypeName, r.TypeName))

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
resp.Diagnostics.Append(req.State.Get(ctx, &object)...)

if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, fmt.Sprintf("Reading %s_%s with ID: %s", r.ProviderTypeName, r.TypeName, state.ID.ValueString()))
tflog.Debug(ctx, fmt.Sprintf("Reading %s_%s with ID: %s", r.ProviderTypeName, r.TypeName, object.ID.ValueString()))

ctx, cancel := crud.HandleTimeout(ctx, state.Timeouts.Read, ReadTimeout*time.Second, &resp.Diagnostics)
ctx, cancel := crud.HandleTimeout(ctx, object.Timeouts.Read, ReadTimeout*time.Second, &resp.Diagnostics)
if cancel == nil {
return
}
defer cancel()

browserSiteListId := state.BrowserSiteListAssignmentID.ValueString()
browserSiteListId := object.BrowserSiteListAssignmentID.ValueString()

browserSite, err := r.client.
Admin().
Expand All @@ -99,17 +99,17 @@ func (r *BrowserSiteResource) Read(ctx context.Context, req resource.ReadRequest
SiteLists().
ByBrowserSiteListId(browserSiteListId).
Sites().
ByBrowserSiteId(state.ID.ValueString()).
ByBrowserSiteId(object.ID.ValueString()).
Get(ctx, nil)

if err != nil {
errors.HandleGraphError(ctx, err, resp, "Read", r.ReadPermissions)
return
}

MapRemoteStateToTerraform(ctx, &state, browserSite)
MapRemoteStateToTerraform(ctx, &object, browserSite)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -119,22 +119,22 @@ func (r *BrowserSiteResource) Read(ctx context.Context, req resource.ReadRequest

// Update handles the Update operation.
func (r *BrowserSiteResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan BrowserSiteResourceModel
var object BrowserSiteResourceModel

tflog.Debug(ctx, fmt.Sprintf("Starting Update of resource: %s_%s", r.ProviderTypeName, r.TypeName))

resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.Plan.Get(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}

ctx, cancel := crud.HandleTimeout(ctx, plan.Timeouts.Update, UpdateTimeout*time.Second, &resp.Diagnostics)
ctx, cancel := crud.HandleTimeout(ctx, object.Timeouts.Update, UpdateTimeout*time.Second, &resp.Diagnostics)
if cancel == nil {
return
}
defer cancel()

requestBody, err := constructResource(ctx, &plan)
requestBody, err := constructResource(ctx, &object)
if err != nil {
resp.Diagnostics.AddError(
"Error constructing browser site",
Expand All @@ -143,7 +143,7 @@ func (r *BrowserSiteResource) Update(ctx context.Context, req resource.UpdateReq
return
}

browserSiteListId := plan.BrowserSiteListAssignmentID.ValueString()
browserSiteListId := object.BrowserSiteListAssignmentID.ValueString()

_, err = r.client.
Admin().
Expand All @@ -152,15 +152,15 @@ func (r *BrowserSiteResource) Update(ctx context.Context, req resource.UpdateReq
SiteLists().
ByBrowserSiteListId(browserSiteListId).
Sites().
ByBrowserSiteId(plan.ID.ValueString()).
ByBrowserSiteId(object.ID.ValueString()).
Patch(ctx, requestBody, nil)

if err != nil {
errors.HandleGraphError(ctx, err, resp, "Update", r.ReadPermissions)
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -170,22 +170,22 @@ func (r *BrowserSiteResource) Update(ctx context.Context, req resource.UpdateReq

// Delete handles the Delete operation.
func (r *BrowserSiteResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data BrowserSiteResourceModel
var object BrowserSiteResourceModel

tflog.Debug(ctx, fmt.Sprintf("Starting deletion of resource: %s_%s", r.ProviderTypeName, r.TypeName))

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
resp.Diagnostics.Append(req.State.Get(ctx, &object)...)
if resp.Diagnostics.HasError() {
return
}

ctx, cancel := crud.HandleTimeout(ctx, data.Timeouts.Delete, DeleteTimeout*time.Second, &resp.Diagnostics)
ctx, cancel := crud.HandleTimeout(ctx, object.Timeouts.Delete, DeleteTimeout*time.Second, &resp.Diagnostics)
if cancel == nil {
return
}
defer cancel()

browserSiteListId := data.BrowserSiteListAssignmentID.ValueString()
browserSiteListId := object.BrowserSiteListAssignmentID.ValueString()

err := r.client.
Admin().
Expand All @@ -194,7 +194,7 @@ func (r *BrowserSiteResource) Delete(ctx context.Context, req resource.DeleteReq
SiteLists().
ByBrowserSiteListId(browserSiteListId).
Sites().
ByBrowserSiteId(data.ID.ValueString()).
ByBrowserSiteId(object.ID.ValueString()).
Delete(ctx, nil)

if err != nil {
Expand Down
Loading

0 comments on commit bbc3dc3

Please sign in to comment.