func()

in internal/meta/base_meta.go [952:1008]


func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, importIdx int) {
	// Import resources
	addr := item.TFAddr.String()
	meta.Logger().Debug("Importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
	// The actual resource type names in telemetry is redacted
	meta.tc.Trace(telemetry.Info, fmt.Sprintf("Importing %s as %s", item.AzureResourceID.TypeString(), addr))

	importResp, diags := meta.tfclient.ImportResourceState(ctx, typ.ImportResourceStateRequest{
		TypeName: item.TFAddr.Type,
		ID:       item.TFResourceId,
	})
	if diags.HasErrors() {
		meta.Logger().Error("Terraform import failed", "tf_addr", item.TFAddr, "error", diags.Err())
		meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s failed", item.AzureResourceID.TypeString()))
		meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
		item.ImportError = diags.Err()
		item.Imported = false
		return
	}
	if len(importResp.ImportedResources) != 1 {
		err := fmt.Errorf("expect 1 resource being imported, got=%d", len(importResp.ImportedResources))
		meta.Logger().Error(err.Error())
		meta.tc.Trace(telemetry.Error, err.Error())
		item.ImportError = err
		item.Imported = false
		return
	}
	res := importResp.ImportedResources[0]
	readResp, diags := meta.tfclient.ReadResource(ctx, typ.ReadResourceRequest{
		TypeName:   res.TypeName,
		PriorState: res.State,
		Private:    res.Private,
	})
	if diags.HasErrors() {
		meta.Logger().Error("Terraform read a resource failed", "tf_addr", item.TFAddr, "error", diags.Err())
		meta.tc.Trace(telemetry.Error, fmt.Sprintf("Reading %s failed", item.AzureResourceID.TypeString()))
		meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
		item.ImportError = diags.Err()
		item.Imported = false
		return
	}

	// Ensure the state is not null
	if readResp.NewState.IsNull() {
		meta.Logger().Error("Cannot import an non-existent resource", "tf_addr", item.TFAddr)
		meta.tc.Trace(telemetry.Error, fmt.Sprintf("Cannot import an non-existent resource: %s", item.AzureResourceID.TypeString()))
		item.ImportError = fmt.Errorf("Cannot import non-existent remote object")
		item.Imported = false
		return
	}

	meta.Logger().Debug("Finish importing a resource", "tf_id", item.TFResourceId, "tf_addr", addr)
	item.State = readResp.NewState
	item.ImportError = nil
	item.Imported = true
	return
}