Skip to content

Commit

Permalink
Merge pull request #154 from cgascoig/fix_151
Browse files Browse the repository at this point in the history
Extend MoRef syntax to allow specifying organisation to search
  • Loading branch information
cgascoig authored May 5, 2024
2 parents 8452c29 + 40f9068 commit b1dce4c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/gen/genutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,30 @@ func GetMoMoRef(client *util.IsctlClient, moref *oapi.MoRef) (map[string]any, er
return mo, nil
}

filter := moref.Filter

// Here we look up the moid of the organisation if the MoRef has the organization set
if moref.Organization != "" {
orgMoRef := oapi.CanonicaliseMoRef(moref.Organization, "organization.Organization.Relationship")
resolvedOrgMoRef, err := GetMoMoRef(client, orgMoRef)
if err != nil {
return nil, fmt.Errorf("error finding organisation: %v", err)
}

orgMoid, err := dyno.GetString(resolvedOrgMoRef, "Moid")
if err != nil {
return nil, fmt.Errorf("error finding organisation: %v", err)
}

filter = fmt.Sprintf("%s and Organization/Moid eq '%s'", filter, orgMoid)
}

op := GetOperationForRelationship(moref.RelationshipType)
if op == nil {
return nil, fmt.Errorf("no operation for relationship %s", moref.RelationshipType)
}

res, err := op.Execute(client, nil, map[string]string{"filter": moref.Filter})
res, err := op.Execute(client, nil, map[string]string{"filter": filter})
if err != nil {
return nil, fmt.Errorf("error executing lookup query: %v", err)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/oapi/oapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func getSchemaProperty(propName string, schema map[string]any) map[string]any {
type MoRef struct {
Filter string
RelationshipType string
Organization string
}

func canonicaliseRelationshipType(rt string) string {
Expand Down Expand Up @@ -138,6 +139,28 @@ func CanonicaliseMoRef(moref string, defaultRelationshipType string) *MoRef {
}
}

// MoRef:ntp.Policy[default\test]
r = regexp.MustCompile(`^MoRef:([\w\.]+)\[([0-9A-Za-z_\-\.]+)\\([0-9A-Za-z_\-\.]+)\]`)
m = r.FindStringSubmatch(moref)
if m != nil {
return &MoRef{
Filter: fmt.Sprintf("Name eq '%s'", m[3]),
RelationshipType: canonicaliseRelationshipType(m[1]),
Organization: m[2],
}
}

// MoRef[default\test]
r = regexp.MustCompile(`^MoRef\[([0-9A-Za-z_\-\.]+)\\([0-9A-Za-z_\-\.]+)\]`)
m = r.FindStringSubmatch(moref)
if m != nil && defaultRelationshipType != "" {
return &MoRef{
Filter: fmt.Sprintf("Name eq '%s'", m[2]),
RelationshipType: defaultRelationshipType,
Organization: m[1],
}
}

r = regexp.MustCompile(`^\s*([0-9A-Za-z_\-\.]+)\s*$`)
m = r.FindStringSubmatch(moref)
if m != nil && defaultRelationshipType != "" {
Expand Down
18 changes: 18 additions & 0 deletions pkg/oapi/oapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ func TestCanonicaliseMoRef(t *testing.T) {
RelationshipType: "organization.Organization",
},
},
{
moref: "MoRef[default\\test]",
defaultRelationshipType: "ntp.Policy",
res: &MoRef{
Filter: "Name eq 'test'",
RelationshipType: "ntp.Policy",
Organization: "default",
},
},
{
moref: "MoRef:iam.EndPointUser[default\\test]",
defaultRelationshipType: "",
res: &MoRef{
Filter: "Name eq 'test'",
RelationshipType: "iam.EndPointUser",
Organization: "default",
},
},
}

for _, test := range tests {
Expand Down
22 changes: 22 additions & 0 deletions tests/orginization.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ TEST_SECTION="Organization"
./build/isctl ${ISCTL_OPTIONS} create iam endpointuser --Name "${TEST_NAME}" --Organization "${TEST_NAME}"
}

@test "${TEST_SECTION}: manually reference an mo in a specific org" {

echo "Creating test IAM endpointuserpolicy in test org"
./build/isctl ${ISCTL_OPTIONS} create iam endpointuserpolicy --Name "${TEST_NAME}" --Organization "${TEST_NAME}"

echo "Creating test IAM endpointuserrole in test org (references endpointuser and endpointuserpolicy in specific org)"
./build/isctl ${ISCTL_OPTIONS} create iam endpointuserrole \
--EndPointRole '[{"ClassId":"mo.MoRef", "Moid": "59684dcb5e468000016525c8"}]' \
--EndPointUser "MoRef[${TEST_NAME}\\${TEST_NAME}]" \
--EndPointUserPolicy "MoRef[${TEST_NAME}\\${TEST_NAME}]" \
--Password hahahahaha

echo "Deleteing test IAM endpointuserrole in test org"
ORG_MOID=$(./build/isctl ${ISCTL_OPTIONS} get organization organization --name "${TEST_NAME}" -o jsonpath='$.Moid'|| echo "")

# Don't need to delete endpointuserrole - it is automatically deleted when the policy is deleted
# ./build/isctl ${ISCTL_OPTIONS} delete iam endpointuserrole moid $(./build/isctl ${ISCTL_OPTIONS} get iam endpointuserrole --filter "Name eq '${TEST_NAME}' and Organization/Moid eq '${ORG_MOID}'" -o 'jsonpath=$[*].Moid'|| echo "")

echo "Deleteing test IAM endpointuserpolicy in test org"
./build/isctl ${ISCTL_OPTIONS} delete iam endpointuserpolicy moid $(./build/isctl ${ISCTL_OPTIONS} get iam endpointuserpolicy --filter "Name eq '${TEST_NAME}' and Organization/Moid eq '${ORG_MOID}'" -o 'jsonpath=$[*].Moid'|| echo "")
}

# This test is disabled as it the API no longer allows NTP policies with the same name in different orgs
# @test "${TEST_SECTION}: manually create duplicate ntp policies" {
# echo "Creating test NTP policy in default org"
Expand Down

0 comments on commit b1dce4c

Please sign in to comment.