Skip to content

Commit

Permalink
Add endpoint privilege management resource construction and settings …
Browse files Browse the repository at this point in the history
…catalog export
  • Loading branch information
ShocOne committed Dec 5, 2024
1 parent 9fb9b81 commit 1d1d0d0
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 824 deletions.
Original file line number Diff line number Diff line change
@@ -1,85 +1,17 @@
package graphBetaEndpointPrivilegeManagement
package construct

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common/construct"
sharedmodels "github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common/shared_models/graph_beta"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
)

// Main entry point to construct the intune settings catalog profile resource for the Terraform provider.
func constructResource(ctx context.Context, data *EndpointPrivilegeManagementResourceModel) (graphmodels.DeviceManagementConfigurationPolicyable, error) {
tflog.Debug(ctx, fmt.Sprintf("Constructing %s resource from model", ResourceName))

requestBody := graphmodels.NewDeviceManagementConfigurationPolicy()

Name := data.Name.ValueString()
requestBody.SetName(&Name)

description := data.Description.ValueString()
requestBody.SetDescription(&description)

platformStr := data.Platforms.ValueString()
var platform graphmodels.DeviceManagementConfigurationPlatforms
switch platformStr {
case "android":
platform = graphmodels.ANDROID_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "androidEnterprise":
platform = graphmodels.ANDROIDENTERPRISE_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "aosp":
platform = graphmodels.AOSP_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "iOS":
platform = graphmodels.IOS_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "linux":
platform = graphmodels.LINUX_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "macOS":
platform = graphmodels.MACOS_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "windows10":
platform = graphmodels.WINDOWS10_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "windows10X":
platform = graphmodels.WINDOWS10X_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
}
requestBody.SetPlatforms(&platform)

var technologiesStr []string
for _, tech := range data.Technologies {
technologiesStr = append(technologiesStr, tech.ValueString())
}
parsedTechnologies, _ := graphmodels.ParseDeviceManagementConfigurationTechnologies(strings.Join(technologiesStr, ","))
requestBody.SetTechnologies(parsedTechnologies.(*graphmodels.DeviceManagementConfigurationTechnologies))

if len(data.RoleScopeTagIds) > 0 {
var tagIds []string
for _, tag := range data.RoleScopeTagIds {
tagIds = append(tagIds, tag.ValueString())
}
requestBody.SetRoleScopeTagIds(tagIds)
} else {
requestBody.SetRoleScopeTagIds([]string{"0"})
}

settings := constructSettingsCatalogSettings(ctx, data.Settings)
requestBody.SetSettings(settings)

if err := construct.DebugLogGraphObject(ctx, fmt.Sprintf("Final JSON to be sent to Graph API for resource %s", ResourceName), requestBody); err != nil {
tflog.Error(ctx, "Failed to debug log object", map[string]interface{}{
"error": err.Error(),
})
}

tflog.Debug(ctx, fmt.Sprintf("Finished constructing %s resource", ResourceName))

return requestBody, nil
}

// constructSettingsCatalogSettings is a helper function to construct the settings catalog settings from the JSON data.
func constructSettingsCatalogSettings(ctx context.Context, settingsJSON types.String) []graphmodels.DeviceManagementConfigurationSettingable {
// ConstructSettingsCatalogSettings is a helper function to construct the settings catalog settings from the JSON data.
func ConstructSettingsCatalogSettings(ctx context.Context, settingsJSON types.String) []graphmodels.DeviceManagementConfigurationSettingable {
tflog.Debug(ctx, "Constructing settings catalog settings")

if err := json.Unmarshal([]byte(settingsJSON.ValueString()), &sharedmodels.DeviceConfigV2GraphServiceModel); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package graphBetaEndpointPrivilegeManagement

import (
"context"
"fmt"
"strings"

"github.com/deploymenttheory/terraform-provider-microsoft365/internal/resources/common/construct"
"github.com/hashicorp/terraform-plugin-log/tflog"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
)

// Main entry point to construct the intune settings catalog profile resource for the Terraform provider.
func constructResource(ctx context.Context, data *EndpointPrivilegeManagementResourceModel) (graphmodels.DeviceManagementConfigurationPolicyable, error) {
tflog.Debug(ctx, fmt.Sprintf("Constructing %s resource from model", ResourceName))

requestBody := graphmodels.NewDeviceManagementConfigurationPolicy()

Name := data.Name.ValueString()
requestBody.SetName(&Name)

description := data.Description.ValueString()
requestBody.SetDescription(&description)

platformStr := data.Platforms.ValueString()
var platform graphmodels.DeviceManagementConfigurationPlatforms
switch platformStr {
case "android":
platform = graphmodels.ANDROID_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "androidEnterprise":
platform = graphmodels.ANDROIDENTERPRISE_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "aosp":
platform = graphmodels.AOSP_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "iOS":
platform = graphmodels.IOS_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "linux":
platform = graphmodels.LINUX_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "macOS":
platform = graphmodels.MACOS_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "windows10":
platform = graphmodels.WINDOWS10_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
case "windows10X":
platform = graphmodels.WINDOWS10X_DEVICEMANAGEMENTCONFIGURATIONPLATFORMS
}
requestBody.SetPlatforms(&platform)

var technologiesStr []string
for _, tech := range data.Technologies {
technologiesStr = append(technologiesStr, tech.ValueString())
}
parsedTechnologies, _ := graphmodels.ParseDeviceManagementConfigurationTechnologies(strings.Join(technologiesStr, ","))
requestBody.SetTechnologies(parsedTechnologies.(*graphmodels.DeviceManagementConfigurationTechnologies))

if len(data.RoleScopeTagIds) > 0 {
var tagIds []string
for _, tag := range data.RoleScopeTagIds {
tagIds = append(tagIds, tag.ValueString())
}
requestBody.SetRoleScopeTagIds(tagIds)
} else {
requestBody.SetRoleScopeTagIds([]string{"0"})
}

settings := construct.ConstructSettingsCatalogSettings(ctx, data.Settings)
requestBody.SetSettings(settings)

if err := construct.DebugLogGraphObject(ctx, fmt.Sprintf("Final JSON to be sent to Graph API for resource %s", ResourceName), requestBody); err != nil {
tflog.Error(ctx, "Failed to debug log object", map[string]interface{}{
"error": err.Error(),
})
}

tflog.Debug(ctx, fmt.Sprintf("Finished constructing %s resource", ResourceName))

return requestBody, nil
}
Loading

0 comments on commit 1d1d0d0

Please sign in to comment.