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
- The Microsoft Graph API's
Post
method returns a generalMobileAppable
type. - 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
- Type Safety: Ensures we're working with the correct type and can access all required methods.
- Error Prevention: Avoids runtime errors from calling methods that don't exist on the general type.
- Specific Mapping: Allows proper mapping of
WinGetApp
-specific fields inMapRemoteStateToTerraform
. - API Flexibility: Enables us to work with specific subtypes while allowing the API to return a general type.
- Terraform Integration: Bridges the gap between the API's general return type and Terraform's need for specific resource types.
Best Practices
- Always perform the type assertion immediately after receiving the resource from the API.
- Include a safety check (
ok
variable) to ensure the type assertion was successful. - Provide clear error messages if the created resource isn't of the expected type.
- 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
- func DownloadImage(url string) ([]byte, error)
- func FetchStoreAppDetails(packageIdentifier string) (string, string, string, string, error)
- func MapRemoteStateToTerraform(ctx context.Context, data *WinGetAppResourceModel, ...)
- func NewWinGetAppResource() resource.Resource
- type WinGetAppInstallExperienceResourceModel
- type WinGetAppResource
- func (r *WinGetAppResource) Configure(ctx context.Context, req resource.ConfigureRequest, ...)
- func (r *WinGetAppResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
- func (r *WinGetAppResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
- func (r *WinGetAppResource) ImportState(ctx context.Context, req resource.ImportStateRequest, ...)
- func (r *WinGetAppResource) Metadata(ctx context.Context, req resource.MetadataRequest, ...)
- func (r *WinGetAppResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, ...)
- func (r *WinGetAppResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
- func (r *WinGetAppResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse)
- func (r *WinGetAppResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
- type WinGetAppResourceModel
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (r *WinGetAppResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse)
Configure sets the client for the resource.
func (*WinGetAppResource) Create ¶
func (r *WinGetAppResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse)
Create handles the Create operation.
func (*WinGetAppResource) Delete ¶
func (r *WinGetAppResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse)
Delete handles the Delete operation.
func (*WinGetAppResource) ImportState ¶
func (r *WinGetAppResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse)
ImportState imports the resource state.
func (*WinGetAppResource) Metadata ¶
func (r *WinGetAppResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse)
Metadata returns the resource type name.
func (*WinGetAppResource) ModifyPlan ¶
func (r *WinGetAppResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse)
ModifyPlan handles plan modification for diff suppression
func (*WinGetAppResource) Read ¶
func (r *WinGetAppResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse)
Read handles the Read operation.
func (*WinGetAppResource) Schema ¶
func (r *WinGetAppResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse)
Schema returns the schema for the resource.
func (*WinGetAppResource) Update ¶
func (r *WinGetAppResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse)
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