diff --git a/internal/resources/common/construct/settings_catalog_settings.go b/internal/resources/common/construct/settings_catalog_settings.go index b296b18b..b65eb5eb 100644 --- a/internal/resources/common/construct/settings_catalog_settings.go +++ b/internal/resources/common/construct/settings_catalog_settings.go @@ -14,21 +14,22 @@ import ( 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 { + var configModel sharedmodels.DeviceConfigV2GraphServiceModel + + if err := json.Unmarshal([]byte(settingsJSON.ValueString()), &configModel); err != nil { tflog.Error(ctx, "Failed to unmarshal settings JSON", map[string]interface{}{ "error": err.Error(), }) return nil } - // Add debug logging after unmarshaling tflog.Debug(ctx, "Unmarshaled settings data", map[string]interface{}{ - "data": sharedmodels.DeviceConfigV2GraphServiceModel, + "data": configModel, }) settingsCollection := make([]graphmodels.DeviceManagementConfigurationSettingable, 0) - for _, detail := range sharedmodels.DeviceConfigV2GraphServiceModel.SettingsDetails { + for _, detail := range configModel.SettingsDetails { baseSetting := graphmodels.NewDeviceManagementConfigurationSetting() switch detail.SettingInstance.ODataType { diff --git a/internal/resources/common/construct/settings_catalog_settings.go.backup b/internal/resources/common/construct/settings_catalog_settings.go.backup new file mode 100644 index 00000000..b65eb5eb --- /dev/null +++ b/internal/resources/common/construct/settings_catalog_settings.go.backup @@ -0,0 +1,765 @@ +package construct + +import ( + "context" + "encoding/json" + + 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" +) + +// 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") + + var configModel sharedmodels.DeviceConfigV2GraphServiceModel + + if err := json.Unmarshal([]byte(settingsJSON.ValueString()), &configModel); err != nil { + tflog.Error(ctx, "Failed to unmarshal settings JSON", map[string]interface{}{ + "error": err.Error(), + }) + return nil + } + + tflog.Debug(ctx, "Unmarshaled settings data", map[string]interface{}{ + "data": configModel, + }) + + settingsCollection := make([]graphmodels.DeviceManagementConfigurationSettingable, 0) + + for _, detail := range configModel.SettingsDetails { + baseSetting := graphmodels.NewDeviceManagementConfigurationSetting() + + switch detail.SettingInstance.ODataType { + // Handle ChoiceSettings + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + instance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + instance.SetOdataType(&detail.SettingInstance.ODataType) + instance.SetSettingDefinitionId(&detail.SettingInstance.SettingDefinitionId) + + if detail.SettingInstance.ChoiceSettingValue != nil { + choiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + choiceValue.SetValue(&detail.SettingInstance.ChoiceSettingValue.Value) + + var children []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, child := range detail.SettingInstance.ChoiceSettingValue.Children { + switch child.ODataType { + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + // Handle SimpleSettingInstance within ChoiceSettingValue + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&child.ODataType) + simpleInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if child.SimpleSettingValue != nil { + switch child.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&child.SimpleSettingValue.ODataType) + + if strValue, ok := child.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + simpleInstance.SetSimpleSettingValue(stringValue) + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&child.SimpleSettingValue.ODataType) + + if numValue, ok := child.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + } + } + children = append(children, simpleInstance) + + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + // Handle nested ChoiceSettingInstance within ChoiceSettingValue + nestedChoiceInstance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + nestedChoiceInstance.SetOdataType(&child.ODataType) + nestedChoiceInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if child.ChoiceSettingValue != nil { + nestedChoiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + nestedChoiceValue.SetValue(&child.ChoiceSettingValue.Value) + + // Process nested children within the nested ChoiceSettingValue + var nestedChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, nestedChild := range child.ChoiceSettingValue.Children { + switch nestedChild.ODataType { + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + nestedChoiceInstance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + nestedChoiceInstance.SetOdataType(&nestedChild.ODataType) + nestedChoiceInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + nestedChildren = append(nestedChildren, nestedChoiceInstance) + + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + nestedSimpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + nestedSimpleInstance.SetOdataType(&nestedChild.ODataType) + nestedSimpleInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + nestedChildren = append(nestedChildren, nestedSimpleInstance) + + // Handle other types if necessary + default: + tflog.Warn(ctx, "Unhandled @odata.type for nested child", map[string]interface{}{ + "odata_type": nestedChild.ODataType, + }) + } + } + + nestedChoiceValue.SetChildren(nestedChildren) + nestedChoiceInstance.SetChoiceSettingValue(nestedChoiceValue) + } + children = append(children, nestedChoiceInstance) + + // Handling for GroupSettingCollection within Choice + case "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance": + groupCollectionInstance := graphmodels.NewDeviceManagementConfigurationGroupSettingCollectionInstance() + groupCollectionInstance.SetOdataType(&child.ODataType) + groupCollectionInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + // Handle group collection value + if len(child.GroupSettingCollectionValue) > 0 { + var groupValues []graphmodels.DeviceManagementConfigurationGroupSettingValueable + for _, groupItem := range child.GroupSettingCollectionValue { + groupValue := graphmodels.NewDeviceManagementConfigurationGroupSettingValue() + + // Process children of each group item (key-value pairs) + var groupChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, groupChild := range groupItem.Children { + if groupChild.ODataType == "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance" { + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&groupChild.ODataType) + simpleInstance.SetSettingDefinitionId(&groupChild.SettingDefinitionId) + + if groupChild.SimpleSettingValue != nil { + switch groupChild.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&groupChild.SimpleSettingValue.ODataType) + if strValue, ok := groupChild.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + simpleInstance.SetSimpleSettingValue(stringValue) + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&groupChild.SimpleSettingValue.ODataType) + if numValue, ok := groupChild.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + } + } + groupChildren = append(groupChildren, simpleInstance) + } + } + + groupValue.SetChildren(groupChildren) + if groupItem.SettingValueTemplateReference != nil { + groupValue.SetSettingValueTemplateReference(groupItem.SettingValueTemplateReference) + } + groupValues = append(groupValues, groupValue) + } + groupCollectionInstance.SetGroupSettingCollectionValue(groupValues) + } + + children = append(children, groupCollectionInstance) + + // For SimpleSettingCollection within Choice + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingCollectionInstance": + simpleCollectionInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingCollectionInstance() + simpleCollectionInstance.SetOdataType(&child.ODataType) + simpleCollectionInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if len(child.SimpleSettingCollectionValue) > 0 { + var values []graphmodels.DeviceManagementConfigurationSimpleSettingValueable + for _, v := range child.SimpleSettingCollectionValue { + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&v.ODataType) + stringValue.SetValue(&v.Value) + values = append(values, stringValue) + } + simpleCollectionInstance.SetSimpleSettingCollectionValue(values) + } + + children = append(children, simpleCollectionInstance) + } + } + + choiceValue.SetChildren(children) + instance.SetChoiceSettingValue(choiceValue) + } + + baseSetting.SetSettingInstance(instance) + + // Handle SimpleSettingCollection + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingCollectionInstance": + instance := graphmodels.NewDeviceManagementConfigurationSimpleSettingCollectionInstance() + instance.SetOdataType(&detail.SettingInstance.ODataType) + instance.SetSettingDefinitionId(&detail.SettingInstance.SettingDefinitionId) + + if len(detail.SettingInstance.SimpleSettingCollectionValue) > 0 { + var values []graphmodels.DeviceManagementConfigurationSimpleSettingValueable + for _, v := range detail.SettingInstance.SimpleSettingCollectionValue { + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&v.ODataType) + stringValue.SetValue(&v.Value) + values = append(values, stringValue) + } + instance.SetSimpleSettingCollectionValue(values) + } + + baseSetting.SetSettingInstance(instance) + + // Handle SimpleSettingInstance + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + instance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + instance.SetOdataType(&detail.SettingInstance.ODataType) + instance.SetSettingDefinitionId(&detail.SettingInstance.SettingDefinitionId) + + if detail.SettingInstance.SimpleSettingValue != nil { + switch detail.SettingInstance.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + value := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + value.SetOdataType(&detail.SettingInstance.SimpleSettingValue.ODataType) + if stringValue, ok := detail.SettingInstance.SimpleSettingValue.Value.(string); ok { + value.SetValue(&stringValue) + } else { + tflog.Error(ctx, "Expected string value but got different type", map[string]interface{}{ + "value": detail.SettingInstance.SimpleSettingValue.Value, + }) + } + instance.SetSimpleSettingValue(value) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + value := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + value.SetOdataType(&detail.SettingInstance.SimpleSettingValue.ODataType) + if intValue, ok := detail.SettingInstance.SimpleSettingValue.Value.(float64); ok { + intVal := int32(intValue) + value.SetValue(&intVal) + } else { + tflog.Error(ctx, "Expected integer value but got different type", map[string]interface{}{ + "value": detail.SettingInstance.SimpleSettingValue.Value, + }) + } + instance.SetSimpleSettingValue(value) + } + } + + baseSetting.SetSettingInstance(instance) + + // Handle ChoiceSettingCollection + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingCollectionInstance": + instance := graphmodels.NewDeviceManagementConfigurationChoiceSettingCollectionInstance() + instance.SetOdataType(&detail.SettingInstance.ODataType) + instance.SetSettingDefinitionId(&detail.SettingInstance.SettingDefinitionId) + + if len(detail.SettingInstance.ChoiceSettingCollectionValue) > 0 { + var collectionValues []graphmodels.DeviceManagementConfigurationChoiceSettingValueable + for _, choiceItem := range detail.SettingInstance.ChoiceSettingCollectionValue { + choiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + choiceValue.SetValue(&choiceItem.Value) + + // Process children within each choice item + var children []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, child := range choiceItem.Children { + childInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + childInstance.SetOdataType(&child.ODataType) + childInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + // Handle SimpleSettingValue based on type (string or integer) + if child.SimpleSettingValue != nil { + if stringValue, ok := child.SimpleSettingValue.Value.(string); ok { + simpleValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + simpleValue.SetOdataType(&child.SimpleSettingValue.ODataType) + simpleValue.SetValue(&stringValue) + childInstance.SetSimpleSettingValue(simpleValue) + } else if intValue, ok := child.SimpleSettingValue.Value.(float64); ok { + intVal := int32(intValue) + intValueSetting := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValueSetting.SetOdataType(&child.SimpleSettingValue.ODataType) + intValueSetting.SetValue(&intVal) + childInstance.SetSimpleSettingValue(intValueSetting) + } + } + children = append(children, childInstance) + } + choiceValue.SetChildren(children) + collectionValues = append(collectionValues, choiceValue) + } + instance.SetChoiceSettingCollectionValue(collectionValues) + } + + baseSetting.SetSettingInstance(instance) + + // Handling for GroupSettingCollection (Level 1) + case "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance": + groupSettingCollectionInstance := graphmodels.NewDeviceManagementConfigurationGroupSettingCollectionInstance() + groupSettingCollectionInstance.SetOdataType(&detail.SettingInstance.ODataType) + groupSettingCollectionInstance.SetSettingDefinitionId(&detail.SettingInstance.SettingDefinitionId) + + if len(detail.SettingInstance.GroupSettingCollectionValue) > 0 { + var groupValues []graphmodels.DeviceManagementConfigurationGroupSettingValueable + + for _, groupSettingValueItem := range detail.SettingInstance.GroupSettingCollectionValue { + groupValue := graphmodels.NewDeviceManagementConfigurationGroupSettingValue() + groupOdataType := "#microsoft.graph.deviceManagementConfigurationGroupSettingValue" + groupValue.SetOdataType(&groupOdataType) + + if groupSettingValueItem.SettingValueTemplateReference != nil { + groupValue.SetSettingValueTemplateReference(groupSettingValueItem.SettingValueTemplateReference) + } + + var children []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, child := range groupSettingValueItem.Children { + switch child.ODataType { + + // For nested group setting collections within group setting collection (Level 2) + case "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance": + nestedGroupSettingCollectionInstance := graphmodels.NewDeviceManagementConfigurationGroupSettingCollectionInstance() + nestedGroupSettingCollectionInstance.SetOdataType(&child.ODataType) + nestedGroupSettingCollectionInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if len(child.GroupSettingCollectionValue) > 0 { + var nestedGroupValues []graphmodels.DeviceManagementConfigurationGroupSettingValueable + for _, nestedGroupItem := range child.GroupSettingCollectionValue { + nestedGroupValue := graphmodels.NewDeviceManagementConfigurationGroupSettingValue() + nestedGroupOdataType := "#microsoft.graph.deviceManagementConfigurationGroupSettingValue" + nestedGroupValue.SetOdataType(&nestedGroupOdataType) + + var nestedChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, nestedChild := range nestedGroupItem.Children { + switch nestedChild.ODataType { + // Handle group settings collection within group setting collection within group setting collection within group setting collection (Level 3) + case "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance": + nestedNestedGroupSettingCollectionInstance := graphmodels.NewDeviceManagementConfigurationGroupSettingCollectionInstance() + nestedNestedGroupSettingCollectionInstance.SetOdataType(&nestedChild.ODataType) + nestedNestedGroupSettingCollectionInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + + if len(nestedChild.GroupSettingCollectionValue) > 0 { + var level3Values []graphmodels.DeviceManagementConfigurationGroupSettingValueable + for _, level3Item := range nestedChild.GroupSettingCollectionValue { + level3Value := graphmodels.NewDeviceManagementConfigurationGroupSettingValue() + level3OdataType := "#microsoft.graph.deviceManagementConfigurationGroupSettingValue" + level3Value.SetOdataType(&level3OdataType) + + var nestedNestedChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, level3Child := range level3Item.Children { + switch level3Child.ODataType { + // Handle choice settings within group setting collection within group setting collection within group setting collection (Level 4) + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + choiceInstance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + choiceInstance.SetOdataType(&level3Child.ODataType) + choiceInstance.SetSettingDefinitionId(&level3Child.SettingDefinitionId) + + if level3Child.ChoiceSettingValue != nil { + choiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + choiceOdataType := "#microsoft.graph.deviceManagementConfigurationChoiceSettingValue" + choiceValue.SetOdataType(&choiceOdataType) + choiceValue.SetValue(&level3Child.ChoiceSettingValue.Value) + + if level3Child.ChoiceSettingValue.SettingValueTemplateReference != nil { + choiceValue.SetSettingValueTemplateReference(level3Child.ChoiceSettingValue.SettingValueTemplateReference) + } + + var choiceChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, choiceChild := range level3Child.ChoiceSettingValue.Children { + switch choiceChild.ODataType { + // Handle simple setting within choice settings within group setting collection within group setting collection within group setting collection (Level 5) + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&choiceChild.ODataType) + simpleInstance.SetSettingDefinitionId(&choiceChild.SettingDefinitionId) + + if choiceChild.SimpleSettingValue != nil { + switch choiceChild.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringOdataType := "#microsoft.graph.deviceManagementConfigurationStringSettingValue" + stringValue.SetOdataType(&stringOdataType) + if strValue, ok := choiceChild.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + if choiceChild.SimpleSettingValue.SettingValueTemplateReference != nil { + stringValue.SetSettingValueTemplateReference(choiceChild.SimpleSettingValue.SettingValueTemplateReference) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intOdataType := "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue" + intValue.SetOdataType(&intOdataType) + if numValue, ok := choiceChild.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + } + } + choiceChildren = append(choiceChildren, simpleInstance) + } + } + choiceValue.SetChildren(choiceChildren) + choiceInstance.SetChoiceSettingValue(choiceValue) + } + nestedNestedChildren = append(nestedNestedChildren, choiceInstance) + // Handle simple settings within group setting collection within group setting collection within group setting collection (Level 4) + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&level3Child.ODataType) + simpleInstance.SetSettingDefinitionId(&level3Child.SettingDefinitionId) + + if level3Child.SimpleSettingValue != nil { + switch level3Child.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&level3Child.SimpleSettingValue.ODataType) + if strValue, ok := level3Child.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + if level3Child.SimpleSettingValue.SettingValueTemplateReference != nil { + stringValue.SetSettingValueTemplateReference(level3Child.SimpleSettingValue.SettingValueTemplateReference) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&level3Child.SimpleSettingValue.ODataType) + if numValue, ok := level3Child.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + if level3Child.SimpleSettingValue.SettingValueTemplateReference != nil { + intValue.SetSettingValueTemplateReference(level3Child.SimpleSettingValue.SettingValueTemplateReference) + } + simpleInstance.SetSimpleSettingValue(intValue) + + case "#microsoft.graph.deviceManagementConfigurationSecretSettingValue": + secretValue := graphmodels.NewDeviceManagementConfigurationSecretSettingValue() + secretValue.SetOdataType(&child.SimpleSettingValue.ODataType) + if strValue, ok := child.SimpleSettingValue.Value.(string); ok { + secretValue.SetValue(&strValue) + if child.SimpleSettingValue.ValueState != "" { + valueState, err := graphmodels.ParseDeviceManagementConfigurationSecretSettingValueState(child.SimpleSettingValue.ValueState) + if err == nil { + secretValue.SetValueState(valueState.(*graphmodels.DeviceManagementConfigurationSecretSettingValueState)) + } + } + } + simpleInstance.SetSimpleSettingValue(secretValue) + } + } + nestedNestedChildren = append(nestedNestedChildren, simpleInstance) + + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingCollectionInstance": + simpleCollectionInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingCollectionInstance() + simpleCollectionInstance.SetOdataType(&level3Child.ODataType) + simpleCollectionInstance.SetSettingDefinitionId(&level3Child.SettingDefinitionId) + + if len(level3Child.SimpleSettingCollectionValue) > 0 { + var values []graphmodels.DeviceManagementConfigurationSimpleSettingValueable + for _, v := range level3Child.SimpleSettingCollectionValue { + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&v.ODataType) + stringValue.SetValue(&v.Value) + if v.SettingValueTemplateReference != nil { + stringValue.SetSettingValueTemplateReference(v.SettingValueTemplateReference) + } + values = append(values, stringValue) + } + simpleCollectionInstance.SetSimpleSettingCollectionValue(values) + } + nestedNestedChildren = append(nestedNestedChildren, simpleCollectionInstance) + } + } + + if level3Item.SettingValueTemplateReference != nil { + level3Value.SetSettingValueTemplateReference(level3Item.SettingValueTemplateReference) + } + level3Value.SetChildren(nestedNestedChildren) + level3Values = append(level3Values, level3Value) + } + nestedNestedGroupSettingCollectionInstance.SetGroupSettingCollectionValue(level3Values) + } + nestedChildren = append(nestedChildren, nestedNestedGroupSettingCollectionInstance) + // Handle Simple setting within group setting collection within group setting collection (Level 3) + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&nestedChild.ODataType) + simpleInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + + if nestedChild.SimpleSettingValue != nil { + switch nestedChild.SimpleSettingValue.ODataType { + + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&nestedChild.SimpleSettingValue.ODataType) + if strValue, ok := nestedChild.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&nestedChild.SimpleSettingValue.ODataType) + if numValue, ok := nestedChild.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + + case "#microsoft.graph.deviceManagementConfigurationSecretSettingValue": + secretValue := graphmodels.NewDeviceManagementConfigurationSecretSettingValue() + secretValue.SetOdataType(&nestedChild.SimpleSettingValue.ODataType) + if strValue, ok := nestedChild.SimpleSettingValue.Value.(string); ok { + secretValue.SetValue(&strValue) + if nestedChild.SimpleSettingValue.ValueState != "" { + valueState, err := graphmodels.ParseDeviceManagementConfigurationSecretSettingValueState(nestedChild.SimpleSettingValue.ValueState) + if err == nil { + secretValue.SetValueState(valueState.(*graphmodels.DeviceManagementConfigurationSecretSettingValueState)) + } + } + } + simpleInstance.SetSimpleSettingValue(secretValue) + } + } + nestedChildren = append(nestedChildren, simpleInstance) + // Handle Simple setting collection within group setting collection within group setting collection (Level 3) + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingCollectionInstance": + simpleCollectionInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingCollectionInstance() + simpleCollectionInstance.SetOdataType(&nestedChild.ODataType) + simpleCollectionInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + + if len(nestedChild.SimpleSettingCollectionValue) > 0 { + var values []graphmodels.DeviceManagementConfigurationSimpleSettingValueable + for _, v := range nestedChild.SimpleSettingCollectionValue { + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&v.ODataType) + stringValue.SetValue(&v.Value) + values = append(values, stringValue) + } + simpleCollectionInstance.SetSimpleSettingCollectionValue(values) + } + nestedChildren = append(nestedChildren, simpleCollectionInstance) + // Handle choice setting within group setting collection within group setting collection (Level 3) + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + choiceInstance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + choiceInstance.SetOdataType(&nestedChild.ODataType) + choiceInstance.SetSettingDefinitionId(&nestedChild.SettingDefinitionId) + + if nestedChild.ChoiceSettingValue != nil { + choiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + choiceOdataType := "#microsoft.graph.deviceManagementConfigurationChoiceSettingValue" + choiceValue.SetOdataType(&choiceOdataType) + choiceValue.SetValue(&nestedChild.ChoiceSettingValue.Value) + + var choiceChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, choiceChild := range nestedChild.ChoiceSettingValue.Children { + switch choiceChild.ODataType { + // Handle simple setting within choice setting within group setting collection within group setting collection (Level 4) + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&choiceChild.ODataType) + simpleInstance.SetSettingDefinitionId(&choiceChild.SettingDefinitionId) + + if choiceChild.SimpleSettingValue != nil { + switch choiceChild.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&choiceChild.SimpleSettingValue.ODataType) + if strValue, ok := choiceChild.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + if choiceChild.SimpleSettingValue.SettingValueTemplateReference != nil { + stringValue.SetSettingValueTemplateReference(choiceChild.SimpleSettingValue.SettingValueTemplateReference) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&choiceChild.SimpleSettingValue.ODataType) + if numValue, ok := choiceChild.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + if choiceChild.SimpleSettingValue.SettingValueTemplateReference != nil { + intValue.SetSettingValueTemplateReference(choiceChild.SimpleSettingValue.SettingValueTemplateReference) + } + simpleInstance.SetSimpleSettingValue(intValue) + + case "#microsoft.graph.deviceManagementConfigurationSecretSettingValue": + secretValue := graphmodels.NewDeviceManagementConfigurationSecretSettingValue() + secretValue.SetOdataType(&choiceChild.SimpleSettingValue.ODataType) + if strValue, ok := choiceChild.SimpleSettingValue.Value.(string); ok { + secretValue.SetValue(&strValue) + if choiceChild.SimpleSettingValue.ValueState != "" { + valueState, err := graphmodels.ParseDeviceManagementConfigurationSecretSettingValueState(choiceChild.SimpleSettingValue.ValueState) + if err == nil { + secretValue.SetValueState(valueState.(*graphmodels.DeviceManagementConfigurationSecretSettingValueState)) + } + } + } + simpleInstance.SetSimpleSettingValue(secretValue) + } + } + choiceChildren = append(choiceChildren, simpleInstance) + } + } + choiceValue.SetChildren(choiceChildren) + choiceInstance.SetChoiceSettingValue(choiceValue) + } + nestedChildren = append(nestedChildren, choiceInstance) + } + } + nestedGroupValue.SetChildren(nestedChildren) + nestedGroupValues = append(nestedGroupValues, nestedGroupValue) + } + nestedGroupSettingCollectionInstance.SetGroupSettingCollectionValue(nestedGroupValues) + } + children = append(children, nestedGroupSettingCollectionInstance) + + // For nested simple setting collections within group setting collection + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingCollectionInstance": + simpleCollectionInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingCollectionInstance() + simpleCollectionInstance.SetOdataType(&child.ODataType) + simpleCollectionInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if len(child.SimpleSettingCollectionValue) > 0 { + var values []graphmodels.DeviceManagementConfigurationSimpleSettingValueable + for _, valueItem := range child.SimpleSettingCollectionValue { + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&valueItem.ODataType) + stringValue.SetValue(&valueItem.Value) + values = append(values, stringValue) + } + simpleCollectionInstance.SetSimpleSettingCollectionValue(values) + } + + children = append(children, simpleCollectionInstance) + + // For nested simple settings within group setting collection + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&child.ODataType) + simpleInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if child.SimpleSettingValue != nil { + switch child.SimpleSettingValue.ODataType { + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&child.SimpleSettingValue.ODataType) + if strValue, ok := child.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&child.SimpleSettingValue.ODataType) + if numValue, ok := child.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + + case "#microsoft.graph.deviceManagementConfigurationSecretSettingValue": + secretValue := graphmodels.NewDeviceManagementConfigurationSecretSettingValue() + secretValue.SetOdataType(&child.SimpleSettingValue.ODataType) + if strValue, ok := child.SimpleSettingValue.Value.(string); ok { + secretValue.SetValue(&strValue) + if child.SimpleSettingValue.ValueState != "" { + valueState, err := graphmodels.ParseDeviceManagementConfigurationSecretSettingValueState(child.SimpleSettingValue.ValueState) + if err == nil { + secretValue.SetValueState(valueState.(*graphmodels.DeviceManagementConfigurationSecretSettingValueState)) + } + } + } + simpleInstance.SetSimpleSettingValue(secretValue) + } + } + children = append(children, simpleInstance) + + // For nested choice settings within group setting collection + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + choiceInstance := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + choiceInstance.SetOdataType(&child.ODataType) + choiceInstance.SetSettingDefinitionId(&child.SettingDefinitionId) + + if child.ChoiceSettingValue != nil { + choiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + choiceValue.SetValue(&child.ChoiceSettingValue.Value) + + var choiceChildren []graphmodels.DeviceManagementConfigurationSettingInstanceable + for _, choiceChild := range child.ChoiceSettingValue.Children { + switch choiceChild.ODataType { + case "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance": + nestedChoice := graphmodels.NewDeviceManagementConfigurationChoiceSettingInstance() + nestedChoice.SetOdataType(&choiceChild.ODataType) + nestedChoice.SetSettingDefinitionId(&choiceChild.SettingDefinitionId) + + if choiceChild.ChoiceSettingValue != nil { + nestedChoiceValue := graphmodels.NewDeviceManagementConfigurationChoiceSettingValue() + nestedChoiceValue.SetValue(&choiceChild.ChoiceSettingValue.Value) + nestedChoiceValue.SetChildren([]graphmodels.DeviceManagementConfigurationSettingInstanceable{}) + nestedChoice.SetChoiceSettingValue(nestedChoiceValue) + } + choiceChildren = append(choiceChildren, nestedChoice) + + case "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance": + simpleInstance := graphmodels.NewDeviceManagementConfigurationSimpleSettingInstance() + simpleInstance.SetOdataType(&choiceChild.ODataType) + simpleInstance.SetSettingDefinitionId(&choiceChild.SettingDefinitionId) + + if choiceChild.SimpleSettingValue != nil { + switch choiceChild.SimpleSettingValue.ODataType { + + case "#microsoft.graph.deviceManagementConfigurationStringSettingValue": + stringValue := graphmodels.NewDeviceManagementConfigurationStringSettingValue() + stringValue.SetOdataType(&choiceChild.SimpleSettingValue.ODataType) + if strValue, ok := choiceChild.SimpleSettingValue.Value.(string); ok { + stringValue.SetValue(&strValue) + } + simpleInstance.SetSimpleSettingValue(stringValue) + + case "#microsoft.graph.deviceManagementConfigurationIntegerSettingValue": + intValue := graphmodels.NewDeviceManagementConfigurationIntegerSettingValue() + intValue.SetOdataType(&choiceChild.SimpleSettingValue.ODataType) + if numValue, ok := choiceChild.SimpleSettingValue.Value.(float64); ok { + int32Value := int32(numValue) + intValue.SetValue(&int32Value) + } + simpleInstance.SetSimpleSettingValue(intValue) + } + } + choiceChildren = append(choiceChildren, simpleInstance) + } + } + choiceValue.SetChildren(choiceChildren) + choiceInstance.SetChoiceSettingValue(choiceValue) + } + children = append(children, choiceInstance) + } + } + + groupValue.SetChildren(children) + groupValues = append(groupValues, groupValue) + } + groupSettingCollectionInstance.SetGroupSettingCollectionValue(groupValues) + } + + baseSetting.SetSettingInstance(groupSettingCollectionInstance) + + } + + settingsCollection = append(settingsCollection, baseSetting) + } + + tflog.Debug(ctx, "Constructed settings collection", map[string]interface{}{ + "count": len(settingsCollection), + }) + + return settingsCollection +} diff --git a/internal/resources/common/shared_models/graph_beta/settings_catalog.go b/internal/resources/common/shared_models/graph_beta/settings_catalog.go index 870a9484..e0fa4a16 100644 --- a/internal/resources/common/shared_models/graph_beta/settings_catalog.go +++ b/internal/resources/common/shared_models/graph_beta/settings_catalog.go @@ -10,7 +10,7 @@ import ( // Keys are ordered alphabetically to ensure consistent ordering. This differs from the graph schema. // This doesn't affect requests to the Graph API, but it ensures that the state is consistent and can be compared // when using plan modifers. -var DeviceConfigV2GraphServiceModel struct { +type DeviceConfigV2GraphServiceModel struct { SettingsDetails []struct { ID string `json:"id"` SettingInstance struct { @@ -203,6 +203,7 @@ var DeviceConfigV2GraphServiceModel struct { SettingValueTemplateReference graphmodels.DeviceManagementConfigurationSettingValueTemplateReferenceable `json:"settingValueTemplateReference"` } `json:"groupSettingCollectionValue,omitempty"` + // Setting instance Odata and template reference ODataType string `json:"@odata.type"` SettingDefinitionId string `json:"settingDefinitionId"` SettingInstanceTemplateReference graphmodels.DeviceManagementConfigurationSettingValueTemplateReferenceable `json:"settingInstanceTemplateReference"`