Skip to content

Commit

Permalink
add a function to create a ResourceGroup unstructured (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Jan 8, 2021
1 parent 8201c64 commit c770c71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pkg/live/rgpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,34 @@ func generateInventoryObj(inv *kptfile.Inventory) (*unstructured.Unstructured, e
return nil, err
}
// Create and return ResourceGroup custom resource as inventory object.
var inventoryObj = ResourceGroupUnstructured(inv.Name, inv.Namespace, inv.InventoryID)
labels := inv.Labels
if labels == nil {
labels = make(map[string]string)
}
labels[common.InventoryLabel] = inv.InventoryID
inventoryObj.SetLabels(labels)
inventoryObj.SetAnnotations(inv.Annotations)
return inventoryObj, nil
}

func ResourceGroupUnstructured(name, namespace, id string) *unstructured.Unstructured {
groupVersion := fmt.Sprintf("%s/%s", ResourceGroupGVK.Group, ResourceGroupGVK.Version)
var inventoryObj = &unstructured.Unstructured{
inventoryObj := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": groupVersion,
"kind": ResourceGroupGVK.Kind,
"metadata": map[string]interface{}{
"name": inv.Name,
"namespace": inv.Namespace,
"name": name,
"namespace": namespace,
"labels": map[string]interface{}{
common.InventoryLabel: inv.InventoryID,
common.InventoryLabel: id,
},
},
"spec": map[string]interface{}{
"resources": []interface{}{},
},
},
}
labels := inv.Labels
if labels == nil {
labels = make(map[string]string)
}
labels[common.InventoryLabel] = inv.InventoryID
inventoryObj.SetLabels(labels)
inventoryObj.SetAnnotations(inv.Annotations)
return inventoryObj, nil
return inventoryObj
}
19 changes: 19 additions & 0 deletions pkg/live/rgpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,22 @@ func getInventoryAnnotations(inv *unstructured.Unstructured) map[string]string {
}
return accessor.GetAnnotations()
}

func TestResourceGroupUnstructured(t *testing.T) {
name := "name"
namespace := "test"
id := "random-id"
rg := ResourceGroupUnstructured(name, namespace, id)
if rg == nil {
t.Fatal("resourcegroup shouldn't be nil")
}
if rg.GetName() != name {
t.Fatalf("resourcegroup name expected %s, but got %s", name, rg.GetName())
}
if rg.GetNamespace() != namespace {
t.Fatalf("resourcegroup namespace expected %s, but got %s", namespace, rg.GetNamespace())
}
if rg.GetLabels()[common.InventoryLabel] != id {
t.Fatalf("resourcegroup inventory id expected %s, but got %s", id, rg.GetLabels()[common.InventoryLabel])
}
}

0 comments on commit c770c71

Please sign in to comment.