Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offload handling property-like resources to aztft #238

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/hashicorp/terraform-exec v0.17.2
github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370
github.com/magodo/azlist v0.0.0-20220923015326-17b16cc9b805
github.com/magodo/aztft v0.1.1-0.20220923054138-3dfe6a12ed5e
github.com/magodo/aztft v0.3.0
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6
github.com/magodo/textinput v0.0.0-20210913072708-7d24f2b4b0c0
github.com/magodo/tfadd v0.10.1-0.20220923051211-d1a1b9297df4
Expand All @@ -41,6 +41,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/datafactory/armdatafactory v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/datashare/armdatashare v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/desktopvirtualization/armdesktopvirtualization v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/devtestlabs/armdevtestlabs v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/digitaltwins/armdigitaltwins v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/domainservices/armdomainservices v1.0.0 // indirect
Expand All @@ -56,6 +57,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/securityinsights/armsecurityinsights/v2 v2.0.0-beta.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagecache/armstoragecache v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagepool/armstoragepool v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/streamanalytics/armstreamanalytics v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/synapse/armsynapse v0.5.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/timeseriesinsights/armtimeseriesinsights v1.0.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprot
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection v1.0.0/go.mod h1:CmZQSRwBPP7KNjDA+PHaoR2m8wgOsbTd9ncqZgSzgHA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/datashare/armdatashare v1.0.0 h1:9xFFUWVgkCIRg42rNq9cnaq/hct+tV5eCwja+1BBcpk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/datashare/armdatashare v1.0.0/go.mod h1:O8rDteaSO14ijw1tEpQ1+h8doTODhRPcbaxT+hqlsRo=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/desktopvirtualization/armdesktopvirtualization v1.0.0 h1:E404espBxzBWw7BlZa7TONlqMEH7P3P8XCe686PSmZQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/desktopvirtualization/armdesktopvirtualization v1.0.0/go.mod h1:XM/9J9tLtumXaGIJwbsbm7WlsZwxOtKB34HwmFW9qyU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/devtestlabs/armdevtestlabs v1.0.0 h1:GuqpJGl+Ua5hVijY/ovYSO+iTf1QHDZDFXtVSLrZeq8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/devtestlabs/armdevtestlabs v1.0.0/go.mod h1:igAw5va/nEHEc8OYWPz+g3y1LdPig04qMS+9aqwtn+w=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/digitaltwins/armdigitaltwins v1.0.0 h1:7zDYZ29qnHH8+QJ+oIIAb1NnBIXE2OmeIraWtd76OZI=
Expand Down Expand Up @@ -61,6 +63,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.0.0/go.mod h1:c+Lifp3EDEamAkPVzMooRNOK6CZjNSdEnf1A7jsI9u4=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagecache/armstoragecache v1.0.0 h1:3UQFOxKO93+20/+4mZweaGTbICtgQDRchk946N6/mvI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagecache/armstoragecache v1.0.0/go.mod h1:3n9xGw4M5LJpJLV62q8elF/XpfC/f4CY0SudWJuQOIQ=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagepool/armstoragepool v1.0.0 h1:x51iJ683OHrnUqfTsRM8Q5yAOb+a08ifYWPc8+fFifc=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storagepool/armstoragepool v1.0.0/go.mod h1:4QQ4coy9WcnRgkB8P1wfhcnHqfhU2rD2NOaqEUmBBno=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/streamanalytics/armstreamanalytics v1.0.0 h1:IDuSHsqMhi6PysfqSycxXBz7WOYRGSvw31IK/Zio8cA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/streamanalytics/armstreamanalytics v1.0.0/go.mod h1:44ZAL1LS24yrReci8LqmY3tY9sX0wf/wXadZNT8SQHY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/synapse/armsynapse v0.5.0 h1:CMHxfGGGbg9ua3uSBNRdhKa+fciYNavayBiG0I6LA34=
Expand Down Expand Up @@ -179,8 +183,8 @@ github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370 h1:n8RrB7jcZ9lQE7tyF2
github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370/go.mod h1:rR8E7zfGMbmfnSQvrkFiWYdhrfTqsVSltelnZB09BwA=
github.com/magodo/azlist v0.0.0-20220923015326-17b16cc9b805 h1:kDaY1/REkmmcBxPJdAETUD/J0MpE++P3Zdc9gGl+x30=
github.com/magodo/azlist v0.0.0-20220923015326-17b16cc9b805/go.mod h1:Zke5PFfagiNep06RgNgMXgpLVW4LafSWJJqSQKQAd80=
github.com/magodo/aztft v0.1.1-0.20220923054138-3dfe6a12ed5e h1:yV2GuHH07JefAUx84efDK43SSDj8/1fBZCoA1m8jbp0=
github.com/magodo/aztft v0.1.1-0.20220923054138-3dfe6a12ed5e/go.mod h1:WvcLoLom5tLx+aVDRbImtg7V/D3avnUEN9YSI3+92n8=
github.com/magodo/aztft v0.3.0 h1:7T+HqyEoDo3WGvTsLVhUeM5iZQJHha34HI2BG5jMCm8=
github.com/magodo/aztft v0.3.0/go.mod h1:tk3eGjxk+d+Y3fcenXT2fWMLGZqzHww4mdxYuvcrfLU=
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6 h1:CElHO4hPXC+Eivy8sUC/WrnH3jmQzdF2x0lEXBEYul8=
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6/go.mod h1:Cn4fFwFH/Ddw9sjWPeSS72bNaxbM+FRXf7pkGEDReq8=
github.com/magodo/textinput v0.0.0-20210913072708-7d24f2b4b0c0 h1:aNtr4iNv/tex2t8W1u3scAoNHEnFlTKhNNHOpYStqbs=
Expand Down
8 changes: 4 additions & 4 deletions internal/meta/meta_res.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func (meta ResMeta) GetAzureResource(ctx context.Context) (map[string]interface{
}

func (meta ResMeta) QueryResourceTypeAndId() (string, string, error) {
lrt, lid, err := aztft.QueryTypeAndId(meta.AzureId.String(), true)
ltype, lid, _, err := aztft.QueryTypeAndId(meta.AzureId.String(), true)
if err != nil {
return "", "", err
}
if len(lrt) != 1 {
return "", "", fmt.Errorf("expect exactly one resource type, got=%d", len(lrt))
if len(ltype) != 1 {
return "", "", fmt.Errorf("expect exactly one resource type, got=%d", len(ltype))
}
if len(lid) != 1 {
return "", "", fmt.Errorf("expect exactly one resource id, got=%d", len(lid))
}
return lrt[0], lid[0], nil
return ltype[0].TFType, lid[0], nil
}

func (meta ResMeta) QueryResourceId(rt string) (string, error) {
Expand Down
53 changes: 24 additions & 29 deletions internal/resourceset/azure_resource_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ type AzureResourceSet struct {
type AzureResource struct {
Id armid.ResourceId
Properties map[string]interface{}

// PesudoResourceInfo is only non-nil for the specially populated resources
PesudoResourceInfo *PesudoResourceInfo
}

type PesudoResourceInfo struct {
Expand All @@ -28,39 +25,37 @@ type PesudoResourceInfo struct {
func (rset AzureResourceSet) ToTFResources() []TFResource {
tfresources := []TFResource{}
for _, res := range rset.Resources {
// This is a TF pesudo resource, whose TF info are already available.
if res.PesudoResourceInfo != nil {
azureId := res.Id.String()
tftypes, tfids, exact, err := aztft.QueryTypeAndId(azureId, true)
if err != nil {
log.Printf("WARNING: Failed to query resource type for %s: %v\n", azureId, err)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.Id,
TFId: res.PesudoResourceInfo.TFId,
TFType: res.PesudoResourceInfo.TFType,
// Use the azure ID as the TF ID as a fallback
TFId: azureId,
})
continue
}

azureId := res.Id.String()
var (
// Use the azure ID as the TF ID as a fallback
tfId = azureId
tfType string
)
tftypes, tfids, err := aztft.QueryTypeAndId(azureId, true)
if err == nil {
if len(tfids) == 1 && len(tftypes) == 1 {
tfId = tfids[0]
tfType = tftypes[0]
} else {
if !exact {
// It is not possible to return multiple result when API is used.
log.Printf("WARNING: No query result for resource type and TF id for %s\n", azureId)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.Id,
// Use the azure ID as the TF ID as a fallback
TFId: azureId,
})
} else {
log.Printf("WARNING: Expect one query result for resource type and TF id for %s, got %d type and %d id.\n", azureId, len(tftypes), len(tfids))
for i := range tfids {
tfresources = append(tfresources, TFResource{
AzureId: tftypes[i].AzureId,
TFId: tfids[i],
TFType: tftypes[i].TFType,
})
}
}
} else {
log.Printf("WARNING: Failed to query resource type for %s: %v\n", azureId, err)
}

tfresources = append(tfresources, TFResource{
AzureId: res.Id,
TFId: tfId,
TFType: tfType,
})
}

sort.Slice(tfresources, func(i, j int) bool {
Expand Down
34 changes: 5 additions & 29 deletions internal/resourceset/azure_resource_set_hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import (
"strings"

"github.com/magodo/armid"
"github.com/magodo/aztft/aztft"

"github.com/tidwall/gjson"
)

// PopulateResourceTypesNeedsBody is a map to record resources that need API body to decide whether to populate.
// This is used in single resource mode to see whether an API call is needed.
var PopulateResourceTypesNeedsBody = map[string]bool{
// PopulateResourceTypes is a map to record resources that might to be populate other resources.
// This is used in single resource mode to decide whether to call API to get its body.
var PopulateResourceTypes = map[string]bool{
"MICROSOFT.COMPUTE/VIRTUALMACHINES": true,
}

// PopulateResource populate single resource for certain Azure resouce type that is known might maps to more than one TF resources.
// In most cases, this step is used to populate the Azure managed resource, or the Terraform pesudo (i.e. association/property-like) resource.
// PopulateResource populate single resource for certain Azure resouce type that is known might maps to more than one TF resources, which are missing from azlist.
// In most cases, this step is used to populate the Azure managed resource.
func (rset *AzureResourceSet) PopulateResource() error {
// Populate managed data disk (and the association) for VMs that are missing from Azure exported resource set.
if err := rset.populateForVirtualMachine(); err != nil {
Expand Down Expand Up @@ -75,29 +74,6 @@ func (rset *AzureResourceSet) populateForVirtualMachine() error {
return fmt.Errorf(`populating managed disks for %q: %v`, res.Id, err)
}
rset.Resources = append(rset.Resources, disks...)

// Add the association resource
for _, disk := range disks {
diskName := disk.Id.Names()[0]

// It doesn't matter using linux/windows below, as their resource ids are the same.
vmTFId, err := aztft.QueryId(res.Id.String(), "azurerm_linux_virtual_machine", false)
if err != nil {
return fmt.Errorf("querying resource id for %s: %v", res.Id, err)
}

azureId := res.Id.Clone().(*armid.ScopedResourceId)
azureId.AttrTypes = append(azureId.AttrTypes, "dataDisks")
azureId.AttrNames = append(azureId.AttrNames, diskName)

rset.Resources = append(rset.Resources, AzureResource{
Id: azureId,
PesudoResourceInfo: &PesudoResourceInfo{
TFType: "azurerm_virtual_machine_data_disk_attachment",
TFId: vmTFId + "/dataDisks/" + diskName,
},
})
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ResourceImport(ctx context.Context, cfg config.ResConfig) error {
// Populating resource requires API body. We only call GET on the known resources because:
// 1. The ARM schema API version might be wrong
// 2. Resoruce mode supports pesudo resources defined by aztft (e.g. key vault certificate), which has no Azure counterpart
if resourceset.PopulateResourceTypesNeedsBody[strings.ToUpper(c.AzureId.TypeString())] {
if resourceset.PopulateResourceTypes[strings.ToUpper(c.AzureId.TypeString())] {
body, err := c.GetAzureResource(ctx)
if err != nil {
return err
Expand Down