graphBetaWinGetApp

package
v0.1.10-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2024 License: MPL-2.0 Imports: 28 Imported by: 0

README

WinGet App Type Assertion in Terraform Provider

This README explains the type assertion process used in our Terraform provider when creating WinGet app resources with the Microsoft Graph API.

Background

When creating a WinGet app resource, we encounter a mismatch between the type returned by the Microsoft Graph API and the specific type we need to work with in our Terraform provider.

The Problem

  1. The Microsoft Graph API's Post method returns a general MobileAppable type.
  2. Our Terraform provider needs to work with the more specific WinGetAppable type to access all necessary methods and properties.

The Solution

We use a type assertion to safely convert the general MobileAppable type to the specific WinGetAppable type.

Code Example
resource, err := r.client.DeviceAppManagement().MobileApps().Post(context.Background(), requestBody, nil)
if err != nil {
    // Error handling...
    return
}

resourceAsWinGetApp, ok := resource.(models.WinGetAppable)
if !ok {
    resp.Diagnostics.AddError(
        "Error creating resource",
        fmt.Sprintf("Created resource is not of type WinGetApp: %s_%s", r.ProviderTypeName, r.TypeName),
    )
    return
}

MapRemoteStateToTerraform(ctx, &plan, resourceAsWinGetApp)

Why This is Necessary

  1. Type Safety: Ensures we're working with the correct type and can access all required methods.
  2. Error Prevention: Avoids runtime errors from calling methods that don't exist on the general type.
  3. Specific Mapping: Allows proper mapping of WinGetApp-specific fields in MapRemoteStateToTerraform.
  4. API Flexibility: Enables us to work with specific subtypes while allowing the API to return a general type.
  5. Terraform Integration: Bridges the gap between the API's general return type and Terraform's need for specific resource types.

Best Practices

  1. Always perform the type assertion immediately after receiving the resource from the API.
  2. Include a safety check (ok variable) to ensure the type assertion was successful.
  3. Provide clear error messages if the created resource isn't of the expected type.
  4. Use the asserted type for all subsequent operations that require WinGetApp-specific functionality.

By following this pattern, we maintain code robustness and ensure correct handling of WinGet app resources in our Terraform provider.

Documentation

Overview

REF: https://learn.microsoft.com/en-us/graph/api/resources/intune-apps-wingetapp?view=graph-rest-beta

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DownloadImage

func DownloadImage(url string) ([]byte, error)

DownloadImage downloads an image from a given URL and returns it as a byte slice

func FetchStoreAppDetails

func FetchStoreAppDetails(packageIdentifier string) (string, string, string, string, error)

FetchStoreAppDetails fetches and parses details from the Microsoft Store webpage based on the packageIdentifier It also extracts the icon URL, app description, and publisher

func MapRemoteStateToTerraform

func MapRemoteStateToTerraform(ctx context.Context, data *WinGetAppResourceModel, remoteResource models.WinGetAppable)

func NewWinGetAppResource

func NewWinGetAppResource() resource.Resource

Types

type MimeContentModel

type MimeContentModel struct {
	Type  types.String `tfsdk:"type"`
	Value types.String `tfsdk:"value"`
}

MimeContentModel represents the MIME content structure

type WinGetAppInstallExperienceModel

type WinGetAppInstallExperienceModel struct {
	RunAsAccount types.String `tfsdk:"run_as_account"`
}

WinGetAppInstallExperienceModel represents the install experience structure

type WinGetAppResource

type WinGetAppResource struct {
	ProviderTypeName string
	TypeName         string
	ReadPermissions  []string
	WritePermissions []string
	// contains filtered or unexported fields
}

func (*WinGetAppResource) Configure

Configure sets the client for the resource.

func (*WinGetAppResource) Create

Create handles the Create operation.

func (*WinGetAppResource) Delete

Delete handles the Delete operation.

func (*WinGetAppResource) GetTypeName

func (r *WinGetAppResource) GetTypeName() string

GetTypeName returns the type name of the resource from the state model.

func (*WinGetAppResource) ImportState

ImportState imports the resource state.

func (*WinGetAppResource) Metadata

Metadata returns the resource type name.

func (*WinGetAppResource) Read

Read handles the Read operation.

func (*WinGetAppResource) Schema

func (*WinGetAppResource) Update

Update handles the Update operation.

type WinGetAppResourceModel

type WinGetAppResourceModel struct {
	ID                    types.String                                                    `tfsdk:"id"`
	DisplayName           types.String                                                    `tfsdk:"display_name"`
	Description           types.String                                                    `tfsdk:"description"`
	Publisher             types.String                                                    `tfsdk:"publisher"`
	LargeIcon             types.Object                                                    `tfsdk:"large_icon"`
	CreatedDateTime       types.String                                                    `tfsdk:"created_date_time"`
	LastModifiedDateTime  types.String                                                    `tfsdk:"last_modified_date_time"`
	IsFeatured            types.Bool                                                      `tfsdk:"is_featured"`
	PrivacyInformationUrl types.String                                                    `tfsdk:"privacy_information_url"`
	InformationUrl        types.String                                                    `tfsdk:"information_url"`
	Owner                 types.String                                                    `tfsdk:"owner"`
	Developer             types.String                                                    `tfsdk:"developer"`
	Notes                 types.String                                                    `tfsdk:"notes"`
	UploadState           types.Int64                                                     `tfsdk:"upload_state"`
	PublishingState       types.String                                                    `tfsdk:"publishing_state"`
	IsAssigned            types.Bool                                                      `tfsdk:"is_assigned"`
	RoleScopeTagIds       []types.String                                                  `tfsdk:"role_scope_tag_ids"`
	DependentAppCount     types.Int64                                                     `tfsdk:"dependent_app_count"`
	SupersedingAppCount   types.Int64                                                     `tfsdk:"superseding_app_count"`
	SupersededAppCount    types.Int64                                                     `tfsdk:"superseded_app_count"`
	ManifestHash          types.String                                                    `tfsdk:"manifest_hash"`
	PackageIdentifier     types.String                                                    `tfsdk:"package_identifier"`
	InstallExperience     *WinGetAppInstallExperienceModel                                `tfsdk:"install_experience"`
	Timeouts              timeouts.Value                                                  `tfsdk:"timeouts"`
	Assignments           []graphBetaMobileAppAssignment.MobileAppAssignmentResourceModel `tfsdk:"assignments"`
}

WinGetAppResourceModel represents the Terraform resource model for a WinGetApp

func (*WinGetAppResourceModel) GetID

func (s *WinGetAppResourceModel) GetID() string

GetID returns the ID of a resource from the state model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL