Skip to content

Commit

Permalink
Add read functionality for firmware repository datasource (#119)
Browse files Browse the repository at this point in the history
* added functionality for firmware repository datasource
  • Loading branch information
AnikaAgiwal2711 authored May 27, 2024
1 parent 506fd72 commit acc6ecf
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 42 deletions.
93 changes: 53 additions & 40 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type GatewayClient struct {
password string
token string
version string
insecure bool
}

// NewGateway returns a new gateway client.
Expand All @@ -62,6 +63,7 @@ func NewGateway(host string, username, password string, insecure, useCerts bool)
host: host,
username: username,
password: password,
insecure: insecure,
}

if insecure {
Expand Down Expand Up @@ -97,64 +99,75 @@ func NewGateway(host string, username, password string, insecure, useCerts bool)
gc.version = version
// No need to create token
} else {
bodyData := map[string]interface{}{
"username": username,
"password": password,
}

body, _ := json.Marshal(bodyData)

req, err := http.NewRequest(http.MethodPost, host+"/rest/auth/login", bytes.NewBuffer(body))
token, err := gc.NewTokenGeneration()
if err != nil {
return nil, err
}

req.Header.Add("Content-Type", "application/json")
gc.token = token

resp, err := gc.http.Do(req)
version, err = gc.GetVersion()
if err != nil {
return nil, err
}
gc.version = version
}

defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
}
}()

// parse the response
switch {
case resp == nil:
return nil, errNilReponse
case !(resp.StatusCode >= 200 && resp.StatusCode <= 299):
return nil, ParseJSONError(resp)
}
return gc, nil
}

bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// NewTokenGeneration return a new token when logged in
func (gc *GatewayClient) NewTokenGeneration() (string, error) {
var token string
bodyData := map[string]interface{}{
"username": gc.username,
"password": gc.password,
}

responseBody := string(bs)
body, _ := json.Marshal(bodyData)

result := make(map[string]interface{})
jsonErr := json.Unmarshal([]byte(responseBody), &result)
if err != nil {
return nil, fmt.Errorf("Error For Uploading Package: %s", jsonErr)
}
req, err := http.NewRequest(http.MethodPost, gc.host+"/rest/auth/login", bytes.NewBuffer(body))
if err != nil {
return "", err
}

token := result["access_token"].(string)
req.Header.Add("Content-Type", "application/json")

gc.token = token
resp, err := gc.http.Do(req)
if err != nil {
return "", err
}

version, err = gc.GetVersion()
if err != nil {
return nil, err
defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
}
gc.version = version
}()

// parse the response
switch {
case resp == nil:
return "", errNilReponse
case !(resp.StatusCode >= 200 && resp.StatusCode <= 299):
return "", ParseJSONError(resp)
}

return gc, nil
bs, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

responseBody := string(bs)

result := make(map[string]interface{})
jsonErr := json.Unmarshal([]byte(responseBody), &result)
if err != nil {
return "", fmt.Errorf("Error For Uploading Package: %s", jsonErr)
}

token = result["access_token"].(string)

return token, nil
}

// GetVersion returns version
Expand Down
14 changes: 13 additions & 1 deletion inttests/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func TestUploadCompliance(t *testing.T) {
if os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT") != "" {
sourceLocation = os.Getenv("GOSCALEIO_COMPLIANCE_ENDPOINT")
}
if os.Getenv("GOSCALEIO_COMPLIANCE_NAME") != "" {
sourceLocation = os.Getenv("GOSCALEIO_COMPLIANCE_NAME")
}
ucParam := &types.UploadComplianceParam{
SourceLocation: sourceLocation,
}
Expand All @@ -35,10 +38,19 @@ func TestUploadCompliance(t *testing.T) {
assert.NotNil(t, details.ID)
assert.NotNil(t, details.State)
time.Sleep(5 * time.Second)
indepthDetails, err := GC.GetUploadComplianceDetails(details.ID)
indepthDetails, err := GC.GetUploadComplianceDetails(details.ID, false)
assert.Nil(t, err)
assert.NotEmpty(t, indepthDetails.ID)
assert.NotEmpty(t, indepthDetails.State)

details2, err2 := GC.GetUploadComplianceDetailsUsingID(details.ID)
assert.Nil(t, err2)
assert.NotNil(t, details2.ID)
assert.NotNil(t, details2.State)
details3, err3 := GC.GetFirmwareRepositoryDetailsUsingName("PowerFlex 4.5.0.0 (14)")
assert.Nil(t, err3)
assert.NotNil(t, details3.ID)
assert.NotNil(t, details3.State)
}

func TestApproveUnsignedFile(t *testing.T) {
Expand Down
85 changes: 85 additions & 0 deletions types/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,88 @@ type UploadComplianceTopologyDetails struct {
DefaultCatalog bool `json:"defaultCatalog"`
State string `json:"state"`
}

// FirmwareRepositoryDetails holds the entire details of firmware repository
type FirmwareRepositoryDetails struct {
ID string `json:"id"`
Name string `json:"name"`
SourceLocation string `json:"sourceLocation"`
SourceType string `json:"sourceType"`
DiskLocation string `json:"diskLocation"`
Filename string `json:"filename"`
Username string `json:"username"`
Password string `json:"password"`
DownloadStatus string `json:"downloadStatus"`
CreatedDate string `json:"createdDate"`
CreatedBy string `json:"createdBy"`
UpdatedDate string `json:"updatedDate"`
UpdatedBy string `json:"updatedBy"`
DefaultCatalog bool `json:"defaultCatalog"`
Embedded bool `json:"embedded"`
State string `json:"state"`
SoftwareComponents []Component `json:"softwareComponents"`
SoftwareBundles []Bundle `json:"softwareBundles"`
BundleCount int `json:"bundleCount"`
ComponentCount int `json:"componentCount"`
UserBundleCount int `json:"userBundleCount"`
Minimal bool `json:"minimal"`
DownloadProgress int `json:"downloadProgress"`
ExtractProgress int `json:"extractProgress"`
FileSizeInGigabytes float64 `json:"fileSizeInGigabytes"`
Signature string `json:"signature"`
Custom bool `json:"custom"`
NeedsAttention bool `json:"needsAttention"`
JobID string `json:"jobId"`
Rcmapproved bool `json:"rcmapproved"`
}

// Component holds the details of firmware components
type Component struct {
ID string `json:"id"`
PackageID string `json:"packageId"`
DellVersion string `json:"dellVersion"`
VendorVersion string `json:"vendorVersion"`
ComponentID string `json:"componentId"`
DeviceID string `json:"deviceId"`
SubDeviceID string `json:"subDeviceId"`
VendorID string `json:"vendorId"`
SubVendorID string `json:"subVendorId"`
CreatedDate string `json:"createdDate"`
CreatedBy string `json:"createdBy"`
UpdatedDate string `json:"updatedDate"`
UpdatedBy string `json:"updatedBy"`
Path string `json:"path"`
HashMd5 string `json:"hashMd5"`
Name string `json:"name"`
Category string `json:"category"`
ComponentType string `json:"componentType"`
OperatingSystem string `json:"operatingSystem"`
SystemIDs []string `json:"systemIDs"`
Custom bool `json:"custom"`
NeedsAttention bool `json:"needsAttention"`
Ignore bool `json:"ignore"`
OriginalComponentID string `json:"originalComponentId"`
FirmwareRepoName string `json:"firmwareRepoName"`
}

// Bundle holds the details of firmware bundles
type Bundle struct {
ID string `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
BundleDate string `json:"bundleDate"`
CreatedDate string `json:"createdDate"`
CreatedBy string `json:"createdBy"`
UpdatedDate string `json:"updatedDate"`
UpdatedBy string `json:"updatedBy"`
Description string `json:"description"`
UserBundle bool `json:"userBundle"`
UserBundlePath string `json:"userBundlePath"`
DeviceType string `json:"deviceType"`
DeviceModel string `json:"deviceModel"`
FwRepositoryID string `json:"fwRepositoryId"`
BundleType string `json:"bundleType"`
Custom bool `json:"custom"`
NeedsAttention bool `json:"needsAttention"`
SoftwareComponents []Component `json:"softwareComponents"`
}
Loading

0 comments on commit acc6ecf

Please sign in to comment.