Skip to content

Commit

Permalink
Merge pull request #127 from dell/DeploymentComplianceReport
Browse files Browse the repository at this point in the history
Get compliance reports for a deployment
  • Loading branch information
nikitajoshi1 authored Jul 8, 2024
2 parents 26a7b35 + b2f3c39 commit b1cdc01
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
15 changes: 15 additions & 0 deletions inttests/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ func TestGetDeployeServiceByName(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, template)
}

func TestGetAllComplianceReports(t *testing.T) {
complianceReport, err := GC.GetServiceComplianceDetails("8aaa806c9035ea9101903ab07ff623b2")
assert.Nil(t, err)
assert.NotNil(t, complianceReport)
}

func TestGetAllComplianceReportsByFilter(t *testing.T) {
complianceReport, err := GC.GetServiceComplianceDetailsByFilter("8aaa806c9035ea9101903ab07ff623b2", "ServiceTag", "VMware-42 0c 5e 1c 03 b5 bf a1-a7 cc 69 60 f9 52 80 fa-SW")
assert.Nil(t, err)
assert.NotNil(t, complianceReport)
// invalid filter
complianceReport, err = GC.GetServiceComplianceDetailsByFilter("8aaa806c9035ea9101903ab07ff623b2", "NotValid", "VMware-42 0c 5e 1c 03 b5 bf a1-a7 cc 69 60 f9 52 80 fa-SW")
assert.Error(t, err)
}
87 changes: 87 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,90 @@ func (gc *GatewayClient) DeleteService(serviceID, serversInInventory, serversMan
}
return nil, fmt.Errorf("Couldn't delete service")
}

// GetServiceComplianceDetails retrieves service compliance details for a given deployment.
func (gc *GatewayClient) GetServiceComplianceDetails(deploymentID string) ([]types.ComplianceReport, error) {
defer TimeSpent("GetServiceComplianceDetails", time.Now())

path := fmt.Sprintf("/Api/V1/Deployment/%v/firmware/compliancereport", deploymentID)

req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}

if gc.version == "4.0" {
req.Header.Set("Authorization", "Bearer "+gc.token)

err := setCookie(req.Header, gc.host)
if err != nil {
return nil, fmt.Errorf("Error while handling cookie: %s", err)
}

} else {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(gc.username+":"+gc.password)))
}

req.Header.Set("Content-Type", "application/json")

client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return nil, httpRespError
}
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Couldn't find compliance report for given deployment")
}

var complianceReports []types.ComplianceReport
responseString, _ := extractString(httpResp)
parseError := json.Unmarshal([]byte(responseString), &complianceReports)
if parseError != nil {
return nil, fmt.Errorf("Error while parsing response data for compliance report: %s", parseError)
}
if len(complianceReports) == 0 {
return nil, fmt.Errorf("Couldn't find compliance report for given deployment")
}

return complianceReports, nil
}

// GetServiceComplianceDetailsByFilter retrieves service compliance details based on a filter and value.
func (gc *GatewayClient) GetServiceComplianceDetailsByFilter(deploymentID, filter, value string) ([]types.ComplianceReport, error) {
defer TimeSpent("GetServiceComplianceDetailsByFilter", time.Now())

complianceReports, err := gc.GetServiceComplianceDetails(deploymentID)
if err != nil || len(complianceReports) == 0 {
return nil, fmt.Errorf("Couldn't find compliance report for the given deployment")
}

filteredComplianceReports := make([]types.ComplianceReport, 0)
for _, complianceReport := range complianceReports {
switch filter {
case "IpAddress":
if complianceReport.IPAddress == value {
filteredComplianceReports = append(filteredComplianceReports, complianceReport)
}
case "ServiceTag":
if complianceReport.ServiceTag == value {
filteredComplianceReports = append(filteredComplianceReports, complianceReport)
}
case "Compliant":
if strconv.FormatBool(complianceReport.Compliant) == value {
filteredComplianceReports = append(filteredComplianceReports, complianceReport)
}
case "HostName":
if complianceReport.HostName == value {
filteredComplianceReports = append(filteredComplianceReports, complianceReport)
}
case "ID":
if complianceReport.ID == value {
filteredComplianceReports = append(filteredComplianceReports, complianceReport)
}
default:
return nil, fmt.Errorf("Invalid filter provided")
}
}

return filteredComplianceReports, nil
}
56 changes: 56 additions & 0 deletions types/v1/serviceTypes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package goscaleio

// ServiceFailedResponse represents the response when a service fails.
Expand Down Expand Up @@ -91,3 +105,45 @@ type ServiceResponse struct {
Messages []Messages `json:"messages,omitempty"`
StatusCode int `json:"statusCode,omitempty"`
}

// ComplianceReport defines the compliance report object for a service.
type ComplianceReport struct {
ServiceTag string `json:"serviceTag,omitempty"`
IPAddress string `json:"ipAddress,omitempty"`
FirmwareRepositoryName string `json:"firmwareRepositoryName,omitempty"`
ComplianceReportComponents []ComplianceReportComponents `json:"firmwareComplianceReportComponents,omitempty"`
Compliant bool `json:"compliant,omitempty"`
DeviceType string `json:"deviceType,omitempty"`
Model string `json:"model,omitempty"`
Available bool `json:"available,omitempty"`
ManagedState string `json:"managedState,omitempty"`
EmbeddedReport bool `json:"embeddedReport,omitempty"`
DeviceState string `json:"deviceState,omitempty"`
ID string `json:"id,omitempty"`
HostName string `json:"hostname,omitempty"`
CanUpdate bool `json:"canUpdate,omitempty"`
}

// ComplianceReportComponents defines the components in the compliance report.
type ComplianceReportComponents struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CurrentVersion ComplianceReportComponentVersionInfo `json:"currentVersion,omitempty"`
TargetVersion ComplianceReportComponentVersionInfo `json:"targetVersion,omitempty"`
Vendor string `json:"vendor,omitempty"`
OperatingSystem string `json:"operatingSystem,omitempty"`
Compliant bool `json:"compliant,omitempty"`
Software bool `json:"software,omitempty"`
RPM bool `json:"rpm,omitempty"`
Oscapable bool `json:"oscompatible,omitempty"`
}

// ComplianceReportComponentVersionInfo defines the version info in the compliance report component.
type ComplianceReportComponentVersionInfo struct {
ID string `json:"id,omitempty"`
FirmwareName string `json:"firmwareName,omitempty"`
FirmwareType string `json:"firmwareType,omitempty"`
FirmwareVersion string `json:"firmwareVersion,omitempty"`
FirmwareLastUpdate string `json:"firmwareLastUpdate,omitempty"`
FirmwareLevel string `json:"firmwareLevel,omitempty"`
}

0 comments on commit b1cdc01

Please sign in to comment.