Skip to content

Commit eff1350

Browse files
Set spec.owner on root resource of import (#4611)
* Add conversion method AsArbitraryOwnerReference * Add conversion methods AsKnownResourceReference * Pass owner to importer for root resources * Use helper methods for child imports * Tidy imports
1 parent 5b365b1 commit eff1350

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

v2/cmd/asoctl/pkg/importresources/importable_arm_resource.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,7 @@ func (i *importableARMResource) getStatus(
473473

474474
o := genruntime.ArbitraryOwnerReference{}
475475
if i.owner != nil {
476-
o = genruntime.ArbitraryOwnerReference{
477-
Group: i.owner.Group,
478-
Kind: i.owner.Kind,
479-
Name: i.owner.Name,
480-
}
476+
o = i.owner.AsArbitraryOwnerReference()
481477
}
482478

483479
err = s.PopulateFromARM(o, reflecthelpers.ValueOfPtr(armStatus)) // TODO: PopulateFromArm expects a value... ick
@@ -569,22 +565,14 @@ func (i *importableARMResource) SetOwner(
569565

570566
// If the owner is an ArbitraryOwnerReference we need to synthesize one
571567
if ownerField.Type() == reflect.PointerTo(reflect.TypeOf(genruntime.ArbitraryOwnerReference{})) {
572-
aor := genruntime.ArbitraryOwnerReference{
573-
Group: owner.Group,
574-
Kind: owner.Kind,
575-
Name: owner.Name,
576-
}
577-
568+
aor := owner.AsArbitraryOwnerReference()
578569
ownerField.Set(reflect.ValueOf(&aor))
579570
return
580571
}
581572

582573
// if the owner is a KnownResourceReference, we need to synthesize one
583574
if ownerField.Type() == reflect.PointerTo(reflect.TypeOf(genruntime.KnownResourceReference{})) {
584-
krr := genruntime.KnownResourceReference{
585-
Name: owner.Name,
586-
}
587-
575+
krr := owner.AsKnownResourceReference()
588576
ownerField.Set(reflect.ValueOf(&krr))
589577
return
590578
}

v2/cmd/asoctl/pkg/importresources/resource_importer.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111

12+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
1213
"github.com/go-logr/logr"
1314
"github.com/rotisserie/eris"
1415
"github.com/sourcegraph/conc"
@@ -18,6 +19,7 @@ import (
1819
"github.com/Azure/azure-service-operator/v2/cmd/asoctl/pkg/importreporter"
1920
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
2021
"github.com/Azure/azure-service-operator/v2/internal/set"
22+
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
2123
)
2224

2325
// ResourceImporter is the entry point for importing resources.
@@ -68,7 +70,12 @@ func (ri *ResourceImporter) Add(importer ImportableResource) {
6870

6971
// AddARMID adds an ARM ID to the list of resources to import.
7072
func (ri *ResourceImporter) AddARMID(armID string) error {
71-
importer, err := NewImportableARMResource(armID, nil /* no owner */, ri.client)
73+
owner, err := ri.createOwnerFor(armID)
74+
if err != nil {
75+
return eris.Wrapf(err, "adding ARMID %s to importer", armID)
76+
}
77+
78+
importer, err := NewImportableARMResource(armID, owner, ri.client)
7279
if err != nil {
7380
return eris.Wrapf(err, "failed to create importer for %q", armID)
7481
}
@@ -370,3 +377,28 @@ func (ri *ResourceImporter) desiredWorkers() int {
370377

371378
return 4
372379
}
380+
381+
func (ri *ResourceImporter) createOwnerFor(
382+
id string,
383+
) (*genruntime.ResourceReference, error) {
384+
armID, err := arm.ParseResourceID(id)
385+
if err != nil {
386+
// Error is already detailed, no need to wrap
387+
return nil, err
388+
}
389+
390+
if armID.Parent == nil {
391+
// there is no parent, so no owner
392+
return nil, nil
393+
}
394+
395+
// Resource groups don't need owners
396+
if armID.ResourceType.String() == arm.ResourceGroupResourceType.String() {
397+
return nil, nil
398+
}
399+
400+
parent := armID.Parent.String()
401+
return &genruntime.ResourceReference{
402+
ARMID: parent,
403+
}, nil
404+
}

v2/pkg/genruntime/resource_reference.go

+32
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,38 @@ func (ref *ResourceReference) AsNamespacedRef(namespace string) NamespacedResour
187187
}
188188
}
189189

190+
// AsArbitraryOwnerReference creates an ArbitraryOwnerReference from this reference.
191+
func (ref *ResourceReference) AsArbitraryOwnerReference() ArbitraryOwnerReference {
192+
// If this is a direct ARM reference, return just the ARM ID
193+
if ref.IsDirectARMReference() {
194+
return ArbitraryOwnerReference{
195+
ARMID: ref.ARMID,
196+
}
197+
}
198+
199+
// Otherwise return GVK
200+
return ArbitraryOwnerReference{
201+
Group: ref.Group,
202+
Kind: ref.Kind,
203+
Name: ref.Name,
204+
}
205+
}
206+
207+
// AsKnownResourceReference creates a KnownResourceReference from this reference.
208+
func (ref *ResourceReference) AsKnownResourceReference() KnownResourceReference {
209+
// If this is a direct ARM reference, return just the ARM ID
210+
if ref.IsDirectARMReference() {
211+
return KnownResourceReference{
212+
ARMID: ref.ARMID,
213+
}
214+
}
215+
216+
// Otherwise return just the name
217+
return KnownResourceReference{
218+
Name: ref.Name,
219+
}
220+
}
221+
190222
// GroupKind returns the GroupKind of the resource reference
191223
func (ref *ResourceReference) GroupKind() schema.GroupKind {
192224
return schema.GroupKind{

0 commit comments

Comments
 (0)