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

enable query orgs #612

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions govcd/filter_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
QueryOrgVdc types.QueryResultOrgVdcRecordType
QueryTask types.QueryResultTaskRecordType
QueryAdminTask types.QueryResultTaskRecordType
QueryOrg types.QueryResultOrgRecordType
)

// getMetadataValue is a generic metadata lookup for all query items
Expand Down Expand Up @@ -230,6 +231,20 @@ func (vm QueryVm) GetMetadataValue(key string) string {
return getMetadataValue(vm.MetaData, key)
}

// --------------------------------------------------------------
// Organization
// --------------------------------------------------------------
func (org QueryOrg) GetHref() string { return org.HREF }
func (org QueryOrg) GetName() string { return org.Name }
func (org QueryOrg) GetType() string { return "organization" }
func (org QueryOrg) GetDate() string { return "" }
func (org QueryOrg) GetIp() string { return "" }
func (org QueryOrg) GetParentId() string { return "" }
func (org QueryOrg) GetParentName() string { return "" }
func (org QueryOrg) GetMetadataValue(key string) string {
return getMetadataValue(org.Metadata, key)
}

// --------------------------------------------------------------
// result conversion
// --------------------------------------------------------------
Expand Down Expand Up @@ -273,6 +288,10 @@ func resultToQueryItems(queryType string, results Results) ([]QueryItem, error)
for i, item := range results.Results.OrgVdcNetworkRecord {
items[i] = QueryOrgVdcNetwork(*item)
}
case types.QtOrg:
for i, item := range results.Results.OrgRecord {
items[i] = QueryOrg(*item)
}
case types.QtCatalog:
for i, item := range results.Results.CatalogRecord {
items[i] = QueryCatalog(*item)
Expand Down
101 changes: 101 additions & 0 deletions govcd/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,107 @@ func (org *Org) GetTaskList() (*types.TasksList, error) {
return nil, fmt.Errorf("link not found")
}

// QueryAllOrgs returns all Org VDCs using query endpoint
func (client *Client) QueryAllOrgs() ([]*types.QueryResultOrgRecordType, error) {
filter := map[string]string{
// "type": "organisation",
}

return client.queryOrgList(filter)
}

// queryOrgList performs an `orgVdc` or `adminOrgVdc` (for System user) and optionally applies filterFields
func (client *Client) queryOrgList(filterFields map[string]string) ([]*types.QueryResultOrgRecordType, error) {
util.Logger.Printf("[DEBUG] queryOrgList with filter %#v", filterFields)
queryType := client.GetQueryType(types.QtOrg)

filter := map[string]string{
"type": queryType,
}

// When a map of filters with non empty keys and values is supplied - apply it
if filterFields != nil {
filterSlice := make([]string, 0)

for filterFieldName, filterFieldValue := range filterFields {
// Do not inject 'org' filter for System user as API returns an error
if !client.IsSysAdmin && filterFieldName == "org" {
continue
}

if filterFieldName != "" && filterFieldValue != "" {
filterText := fmt.Sprintf("%s==%s", filterFieldName, url.QueryEscape(filterFieldValue))
filterSlice = append(filterSlice, filterText)
}
}

if len(filterSlice) > 0 {
filter["filter"] = strings.Join(filterSlice, ";")
filter["filterEncoded"] = "true"
}
}

results, err := client.cumulativeQuery(queryType, nil, filter)
if err != nil {
return nil, fmt.Errorf("error querying Org VDCs %s", err)
}

if client.IsSysAdmin {
return results.Results.OrgRecord, nil
} else {
return results.Results.OrgRecord, nil
}
}

// QueryOrgByName retrieves an Org
func (client *Client) QueryOrgByName(name string) (*types.QueryResultOrgRecordType, error) {
return client.queryOrgByName(name)
}

// queryOrgByName returns a single QueryResultOrgRecordType
func (client *Client) queryOrgByName(orgName string) (*types.QueryResultOrgRecordType, error) {
filterMap := map[string]string{
"filter": "name==" + orgName,
}
allOrgs, err := client.queryOrgList(filterMap)
if err != nil {
return nil, err
}

if allOrgs == nil || len(allOrgs) < 1 {
return nil, ErrorEntityNotFound
}

if len(allOrgs) > 1 {
return nil, fmt.Errorf("found more than 1 Org with Name '%s'", orgName)
}

return allOrgs[0], nil
}

// QueryOrgByID retrieves an Org
func (client *Client) QueryOrgByID(id string) (*types.QueryResultOrgRecordType, error) {
return client.queryOrgByID(id)
}

// queryOrgByID returns a single QueryResultOrgRecordType
func (client *Client) queryOrgByID(orgId string) (*types.QueryResultOrgRecordType, error) {
filterMap := map[string]string{
"id": orgId,
}
allOrgs, err := client.queryOrgList(filterMap)

if err != nil {
return nil, err
}

if len(allOrgs) < 1 {
return nil, ErrorEntityNotFound
}

return allOrgs[0], nil
}

// queryOrgVdcByName returns a single QueryResultOrgVdcRecordType
func (org *Org) queryOrgVdcByName(vdcName string) (*types.QueryResultOrgVdcRecordType, error) {
filterFields := map[string]string{
Expand Down
3 changes: 3 additions & 0 deletions govcd/query_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func queryFieldsOnDemand(queryType string) ([]string, error) {
"cpuOverheadMhz", "isVCEnabled", "memoryReservedMB", "cpuReservedMhz", "storageOverheadMB", "memoryOverheadMB", "vc"}
taskFields = []string{"href", "id", "type", "org", "orgName", "name", "operationFull", "message", "startDate",
"endDate", "status", "progress", "ownerName", "object", "objectType", "objectName", "serviceNamespace"}
orgFields = []string{"href", "id", "type", "name", "displayName", "isEnabled", "isReadOnly", "canPublishCatalogs",
"deployedVMQuota", "storedVMQuota", "numberOfCatalogs", "numberOfVdcs", "numberOfVApps", "numberOfGroups", "numberOfDisks"}
fieldsOnDemand = map[string][]string{
types.QtVappTemplate: vappTemplatefields,
types.QtAdminVappTemplate: vappTemplatefields,
Expand All @@ -97,6 +99,7 @@ func queryFieldsOnDemand(queryType string) ([]string, error) {
types.QtAdminOrgVdc: orgVdcFields,
types.QtTask: taskFields,
types.QtAdminTask: taskFields,
types.QtOrg: orgFields,
}
)

Expand Down
60 changes: 60 additions & 0 deletions govcd/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,63 @@ func (vcd *TestVCD) TestQueryAllVdcs(check *C) {
check.Assert(contains(knownVdcName, foundVdcNames), Equals, true)
}
}

// Tests Org retrieval by name, by ID, and by a combination of name and ID
func (vcd *TestVCD) Test_QueryOrgByName(check *C) {
vcd.skipIfNotSysAdmin(check)
if vcd.config.VCD.Org.Name == "" {
check.Skip("Test_QueryOrgByName: Org Name not given")
return
}

Orgs, err := vcd.client.QueryOrgByName(vcd.config.Org.Name)
check.Assert(err, IsNil)
check.Assert(len(Orgs) > 0, Equals, true)

orgFound := false
for _, org := range Orgs {
if vcd.config.VCD.Org.Name == org.Name {
orgFound = true
}

if testVerbose {
org
fmt.Printf("Org %s\n", org.Name)
fmt.Printf("\t href %s\n", org.HREF)
fmt.Printf("\t status %s\n", org.Status)
fmt.Printf("\t enabled %v\n", org.IsEnabled)
fmt.Println("")
}
}
check.Assert(orgFound, Equals, true)
}

// Tests Org retrieval by name, by ID, and by a combination of name and ID
func (vcd *TestVCD) Test_QueryOrgById(check *C) {
medea61 marked this conversation as resolved.
Show resolved Hide resolved
vcd.skipIfNotSysAdmin(check)
if vcd.config.VCD.Org.Id == "" {
check.Skip("Test_QueryOrgById: Org Id not given")
return
}

Orgs, err := vcd.client.QueryOrgById(vcd.config.Org.Id)
check.Assert(err, IsNil)
check.Assert(len(Orgs) > 0, Equals, true)

orgFound := false
for _, org := range Orgs {
if vcd.config.VCD.Org.Name == org.Name {
orgFound = true
}

if testVerbose {
org
fmt.Printf("Org %s\n", org.Name)
fmt.Printf("\t href %s\n", org.HREF)
fmt.Printf("\t status %s\n", org.Status)
fmt.Printf("\t enabled %v\n", org.IsEnabled)
fmt.Println("")
medea61 marked this conversation as resolved.
Show resolved Hide resolved
}
}
check.Assert(orgFound, Equals, true)
}
2 changes: 2 additions & 0 deletions types/v56/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ const (
QtVappTemplate = "vAppTemplate" // vApp template
QtAdminVappTemplate = "adminVAppTemplate" // vApp template as admin
QtEdgeGateway = "edgeGateway" // edge gateway
QtOrg = "organization" // Organization
QtOrgVdcNetwork = "orgVdcNetwork" // Org VDC network
QtCatalog = "catalog" // catalog
QtAdminCatalog = "adminCatalog" // catalog as admin
Expand All @@ -283,6 +284,7 @@ const (
// AdminQueryTypes returns the corresponding "admin" query type for each regular type
var AdminQueryTypes = map[string]string{
QtEdgeGateway: QtEdgeGateway, // EdgeGateway query type is the same for admin and regular users
QtOrg: QtOrg, // Organisation query is admin per default
QtOrgVdcNetwork: QtOrgVdcNetwork, // Org VDC Network query type is the same for admin and regular users
QtVappTemplate: QtAdminVappTemplate,
QtCatalog: QtAdminCatalog,
Expand Down
22 changes: 22 additions & 0 deletions types/v56/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,7 @@ type QueryResultRecordsType struct {
VmGroupsRecord []*QueryResultVmGroupsRecordType `xml:"VmGroupsRecord"` // A record representing a VM Group
TaskRecord []*QueryResultTaskRecordType `xml:"TaskRecord"` // A record representing a Task
AdminTaskRecord []*QueryResultTaskRecordType `xml:"AdminTaskRecord"` // A record representing an Admin Task
OrgRecord []*QueryResultOrgRecordType `xml:"OrgRecord"` // A record representing an Organisation
}

// QueryResultVmGroupsRecordType represent a VM Groups record
Expand Down Expand Up @@ -3357,6 +3358,27 @@ type QueryResultTaskRecordType struct {
Metadata *Metadata `xml:"Metadata,omitempty"`
}

// QueryResultOrgVdcRecordType represents an Organisation record
type QueryResultOrgRecordType struct {
HREF string `xml:"href,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name,attr"`
DisplayName string `xml:"displayName,attr,omitempty"`
IsEnabled bool `xml:"IsEnabled,attr,omitempty"`
medea61 marked this conversation as resolved.
Show resolved Hide resolved
IsReadOnly bool `xml:"isReadOnly,attr,omitempty"`
CanPublishCatalogs bool `xml:"canPublishCatalogs,attr,omitempty"`
DeployedVMQuota *int `xml:"deployedVMQuota,attr,omitempty"`
StoredVMQuota *int `xml:"storedVMQuota,attr,omitempty"`
NumberOfCatalogs *int `xml:"numberOfCatalogs,attr,omitempty"`
NumberOfVdcs *int `xml:"numberOfVdcs,attr,omitempty"`
NumberOfVApps *int `xml:"numberOfVApps,attr,omitempty"`
NumberOfGroups *int `xml:"numberOfGroups,attr,omitempty"`
NumberOfDisks *int `xml:"numberOfDisks,attr,omitempty"`
Link *LinkList `xml:"Link,omitempty"`
Metadata *Metadata `xml:"Metadata,omitempty"`
}

// ProviderVdcCreation contains the data needed to create a provider VDC.
// Note that this is a subset of the full structure of a provider VDC.
type ProviderVdcCreation struct {
Expand Down