graphBetaWinGetApp

package
v0.6.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MPL-2.0 Imports: 27 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

View Source
const (
	ResourceName  = "graph_beta_device_and_app_management_win_get_app"
	CreateTimeout = 180
	UpdateTimeout = 180
	ReadTimeout   = 180
	DeleteTimeout = 180
)

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 by performing an HTTP GET request to download the image with redirect support

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 by parsing the HTML content with goquery

func MapRemoteStateToTerraform

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

func NewWinGetAppResource

func NewWinGetAppResource() resource.Resource

Types

type WinGetAppInstallExperienceResourceModel

type WinGetAppInstallExperienceResourceModel 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
	ResourcePath     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) ImportState

ImportState imports the resource state.

func (*WinGetAppResource) Metadata

Metadata returns the resource type name.

func (*WinGetAppResource) ModifyPlan

ModifyPlan handles plan modification for diff suppression

func (*WinGetAppResource) Read

Read handles the Read operation.

func (*WinGetAppResource) Schema

Schema returns the schema for the resource.

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     *WinGetAppInstallExperienceResourceModel `tfsdk:"install_experience"`
	Timeouts              timeouts.Value                           `tfsdk:"timeouts"`
}

WinGetAppResourceModel represents the Terraform resource model for a WinGetApp

Jump to

Keyboard shortcuts

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