Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
53 changes: 37 additions & 16 deletions iq/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nexusiq

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -44,13 +45,12 @@ type Application struct {
} `json:"applicationTags,omitempty"`
}

// GetApplicationByPublicID returns details on the named IQ application
func GetApplicationByPublicID(iq IQ, applicationPublicID string) (*Application, error) {
func GetApplicationByPublicIDContext(ctx context.Context, iq IQ, applicationPublicID string) (*Application, error) {
doError := func(err error) error {
return fmt.Errorf("application '%s' not found: %v", applicationPublicID, err)
}
endpoint := fmt.Sprintf(restApplicationByPublic, applicationPublicID)
body, _, err := iq.Get(endpoint)
body, _, err := iq.Get(ctx, endpoint)
if err != nil {
return nil, doError(err)
}
Expand All @@ -67,8 +67,12 @@ func GetApplicationByPublicID(iq IQ, applicationPublicID string) (*Application,
return &resp.Applications[0], nil
}

// CreateApplication creates an application in IQ with the given name and identifier
func CreateApplication(iq IQ, name, id, organizationID string) (string, error) {
// GetApplicationByPublicID returns details on the named IQ application
func GetApplicationByPublicID(iq IQ, applicationPublicID string) (*Application, error) {
return GetApplicationByPublicIDContext(context.Background(), iq, applicationPublicID)
}

func CreateApplicationContext(ctx context.Context, iq IQ, name, id, organizationID string) (string, error) {
if name == "" || id == "" || organizationID == "" {
return "", fmt.Errorf("cannot create application with empty values")
}
Expand All @@ -82,7 +86,7 @@ func CreateApplication(iq IQ, name, id, organizationID string) (string, error) {
return doError(err)
}

body, _, err := iq.Post(restApplication, bytes.NewBuffer(request))
body, _, err := iq.Post(ctx, restApplication, bytes.NewBuffer(request))
if err != nil {
return doError(err)
}
Expand All @@ -95,17 +99,25 @@ func CreateApplication(iq IQ, name, id, organizationID string) (string, error) {
return resp.ID, nil
}

// DeleteApplication deletes an application in IQ with the given id
func DeleteApplication(iq IQ, applicationID string) error {
if resp, err := iq.Del(fmt.Sprintf("%s/%s", restApplication, applicationID)); err != nil && resp.StatusCode != http.StatusNoContent {
// CreateApplication creates an application in IQ with the given name and identifier
func CreateApplication(iq IQ, name, id, organizationID string) (string, error) {
return CreateApplicationContext(context.Background(), iq, name, id, organizationID)
}

func DeleteApplicationContext(ctx context.Context, iq IQ, applicationID string) error {
if resp, err := iq.Del(ctx, fmt.Sprintf("%s/%s", restApplication, applicationID)); err != nil && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("application '%s' not deleted: %v", applicationID, err)
}
return nil
}

// GetAllApplications returns a slice of all of the applications in an IQ instance
func GetAllApplications(iq IQ) ([]Application, error) {
body, _, err := iq.Get(restApplication)
// DeleteApplication deletes an application in IQ with the given id
func DeleteApplication(iq IQ, applicationID string) error {
return DeleteApplicationContext(context.Background(), iq, applicationID)
}

func GetAllApplicationsContext(ctx context.Context, iq IQ) ([]Application, error) {
body, _, err := iq.Get(ctx, restApplication)
if err != nil {
return nil, fmt.Errorf("applications not found: %v", err)
}
Expand All @@ -118,14 +130,18 @@ func GetAllApplications(iq IQ) ([]Application, error) {
return resp.Applications, nil
}

// GetApplicationsByOrganization returns all applications under a given organization
func GetApplicationsByOrganization(iq IQ, organizationName string) ([]Application, error) {
org, err := GetOrganizationByName(iq, organizationName)
// GetAllApplications returns a slice of all of the applications in an IQ instance
func GetAllApplications(iq IQ) ([]Application, error) {
return GetAllApplicationsContext(context.Background(), iq)
}

func GetApplicationsByOrganizationContext(ctx context.Context, iq IQ, organizationName string) ([]Application, error) {
org, err := GetOrganizationByNameContext(ctx, iq, organizationName)
if err != nil {
return nil, fmt.Errorf("organization not found: %v", err)
}

apps, err := GetAllApplications(iq)
apps, err := GetAllApplicationsContext(ctx, iq)
if err != nil {
return nil, fmt.Errorf("could not get applications list: %v", err)
}
Expand All @@ -139,3 +155,8 @@ func GetApplicationsByOrganization(iq IQ, organizationName string) ([]Applicatio

return orgApps, nil
}

// GetApplicationsByOrganization returns all applications under a given organization
func GetApplicationsByOrganization(iq IQ, organizationName string) ([]Application, error) {
return GetApplicationsByOrganizationContext(context.Background(), iq, organizationName)
}
19 changes: 10 additions & 9 deletions iq/application_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nexusiq

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestGetAllApplications(t *testing.T) {
iq, mock := applicationTestIQ(t)
defer mock.Close()

applications, err := GetAllApplications(iq)
applications, err := GetAllApplicationsContext(context.Background(), iq)
if err != nil {
t.Error(err)
}
Expand All @@ -133,7 +134,7 @@ func TestGetApplicationByPublicID(t *testing.T) {

dummyAppsIdx := 2

got, err := GetApplicationByPublicID(iq, dummyApps[dummyAppsIdx].PublicID)
got, err := GetApplicationByPublicIDContext(context.Background(), iq, dummyApps[dummyAppsIdx].PublicID)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestCreateApplication(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateApplication(tt.args.iq, tt.args.name, tt.args.id, tt.args.organizationID)
got, err := CreateApplicationContext(context.Background(), tt.args.iq, tt.args.name, tt.args.id, tt.args.organizationID)
if (err != nil) != tt.wantErr {
t.Errorf("CreateApplication() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -216,16 +217,16 @@ func TestDeleteApplication(t *testing.T) {
deleteMeApp := Application{PublicID: "deleteMeApp", Name: "deleteMeApp", OrganizationID: "deleteMeAppOrgId"}

var err error
deleteMeApp.ID, err = CreateApplication(iq, deleteMeApp.Name, deleteMeApp.PublicID, deleteMeApp.OrganizationID)
deleteMeApp.ID, err = CreateApplicationContext(context.Background(), iq, deleteMeApp.Name, deleteMeApp.PublicID, deleteMeApp.OrganizationID)
if err != nil {
t.Fatal(err)
}

if err := DeleteApplication(iq, deleteMeApp.PublicID); err != nil {
if err := DeleteApplicationContext(context.Background(), iq, deleteMeApp.PublicID); err != nil {
t.Fatal(err)
}

if _, err := GetApplicationByPublicID(iq, deleteMeApp.PublicID); err == nil {
if _, err := GetApplicationByPublicIDContext(context.Background(), iq, deleteMeApp.PublicID); err == nil {
t.Fatal("App was not deleted")
}
}
Expand Down Expand Up @@ -254,7 +255,7 @@ func TestGetApplicationsByOrganization(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetApplicationsByOrganization(tt.args.iq, tt.args.organizationName)
got, err := GetApplicationsByOrganizationContext(context.Background(), tt.args.iq, tt.args.organizationName)
if (err != nil) != tt.wantErr {
t.Errorf("GetApplicationsByOrganization() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -272,7 +273,7 @@ func ExampleGetAllApplications() {
panic(err)
}

applications, err := GetAllApplications(iq)
applications, err := GetAllApplicationsContext(context.Background(), iq)
if err != nil {
panic(err)
}
Expand All @@ -286,7 +287,7 @@ func ExampleCreateApplication() {
panic(err)
}

appID, err := CreateApplication(iq, "name", "id", "organization")
appID, err := CreateApplicationContext(context.Background(), iq, "name", "id", "organization")
if err != nil {
panic(err)
}
Expand Down
45 changes: 31 additions & 14 deletions iq/componentDetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nexusiq

import (
"bytes"
"context"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -42,17 +43,20 @@ type ComponentDetail struct {
} `json:"securityData"`
}

// GetComponent returns information on a named component
func GetComponent(iq IQ, component Component) (ComponentDetail, error) {
deets, err := GetComponents(iq, []Component{component})
func GetComponentContext(ctx context.Context, iq IQ, component Component) (ComponentDetail, error) {
deets, err := GetComponentsContext(ctx, iq, []Component{component})
if deets == nil || len(deets) == 0 {
return ComponentDetail{}, err
}
return deets[0], err
}

// GetComponents returns information on the named components
func GetComponents(iq IQ, components []Component) ([]ComponentDetail, error) {
// GetComponent returns information on a named component
func GetComponent(iq IQ, component Component) (ComponentDetail, error) {
return GetComponentContext(context.Background(), iq, component)
}

func GetComponentsContext(ctx context.Context, iq IQ, components []Component) ([]ComponentDetail, error) {
reqComponents := detailsRequest{Components: make([]componentRequested, len(components))}
for i, c := range components {
reqComponents.Components[i] = componentRequestedFromComponent(c)
Expand All @@ -63,7 +67,7 @@ func GetComponents(iq IQ, components []Component) ([]ComponentDetail, error) {
return nil, fmt.Errorf("could not generate request: %v", err)
}

body, _, err := iq.Post(restComponentDetails, bytes.NewBuffer(req))
body, _, err := iq.Post(ctx, restComponentDetails, bytes.NewBuffer(req))
if err != nil {
return nil, fmt.Errorf("could not find component details: %v", err)
}
Expand All @@ -76,13 +80,17 @@ func GetComponents(iq IQ, components []Component) ([]ComponentDetail, error) {
return resp.ComponentDetails, nil
}

// GetComponentsByApplication returns an array with all components along with their
func GetComponentsByApplication(iq IQ, appPublicID string) ([]ComponentDetail, error) {
// GetComponents returns information on the named components
func GetComponents(iq IQ, components []Component) ([]ComponentDetail, error) {
return GetComponentsContext(context.Background(), iq, components)
}

func GetComponentsByApplicationContext(ctx context.Context, iq IQ, appPublicID string) ([]ComponentDetail, error) {
componentHashes := make(map[string]struct{})
components := make([]Component, 0)
stages := []Stage{StageBuild, StageStageRelease, StageRelease, StageOperate}
for _, stage := range stages {
if report, err := GetRawReportByAppID(iq, appPublicID, string(stage)); err == nil {
if report, err := GetRawReportByAppIDContext(ctx, iq, appPublicID, string(stage)); err == nil {
for _, c := range report.Components {
if _, ok := componentHashes[c.Hash]; !ok {
componentHashes[c.Hash] = struct{}{}
Expand All @@ -92,12 +100,16 @@ func GetComponentsByApplication(iq IQ, appPublicID string) ([]ComponentDetail, e
}
}

return GetComponents(iq, components)
return GetComponentsContext(ctx, iq, components)
}

// GetAllComponents returns an array with all components along with their
func GetAllComponents(iq IQ) ([]ComponentDetail, error) {
apps, err := GetAllApplications(iq)
// GetComponentsByApplication returns an array with all components along with their
func GetComponentsByApplication(iq IQ, appPublicID string) ([]ComponentDetail, error) {
return GetComponentsByApplicationContext(context.Background(), iq, appPublicID)
}

func GetAllComponentsContext(ctx context.Context, iq IQ) ([]ComponentDetail, error) {
apps, err := GetAllApplicationsContext(ctx, iq)
if err != nil {
return nil, err
}
Expand All @@ -106,7 +118,7 @@ func GetAllComponents(iq IQ) ([]ComponentDetail, error) {
components := make([]ComponentDetail, 0)

for _, app := range apps {
appComponents, err := GetComponentsByApplication(iq, app.PublicID)
appComponents, err := GetComponentsByApplicationContext(ctx, iq, app.PublicID)
// TODO: catcher
if err != nil {
return nil, err
Expand All @@ -122,3 +134,8 @@ func GetAllComponents(iq IQ) ([]ComponentDetail, error) {

return components, nil
}

// GetAllComponents returns an array with all components
func GetAllComponents(iq IQ) ([]ComponentDetail, error) {
return GetAllComponentsContext(context.Background(), iq)
}
21 changes: 11 additions & 10 deletions iq/componentDetails_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nexusiq

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -88,13 +89,13 @@ func TestGetComponent(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetComponent(tt.args.iq, tt.args.component)
got, err := GetComponentContext(context.Background(), tt.args.iq, tt.args.component)
if (err != nil) != tt.wantErr {
t.Errorf("GetComponent() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetComponentContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetComponent() = %v, want %v", got, tt.want)
t.Errorf("GetComponentContext() = %v, want %v", got, tt.want)
}
})
}
Expand All @@ -106,7 +107,7 @@ func TestGetComponents(t *testing.T) {

expected := dummyComponentDetails[0]

details, err := GetComponents(iq, []Component{expected.Component})
details, err := GetComponentsContext(context.Background(), iq, []Component{expected.Component})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -145,13 +146,13 @@ func TestGetComponentsByApplication(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetComponentsByApplication(tt.args.iq, tt.args.appPublicID)
got, err := GetComponentsByApplicationContext(context.Background(), tt.args.iq, tt.args.appPublicID)
if (err != nil) != tt.wantErr {
t.Errorf("GetComponentsByApplication() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetComponentsByApplicationContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetComponentsByApplication() = %v, want %v", got, tt.want)
t.Errorf("GetComponentsByApplicationContext() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -180,13 +181,13 @@ func TestGetAllComponents(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllComponents(tt.args.iq)
got, err := GetAllComponentsContext(context.Background(), tt.args.iq)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllComponents() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllComponentsContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllComponents() = %v, want %v", got, tt.want)
t.Errorf("GetAllComponentsContext() = %v, want %v", got, tt.want)
}
})
}
Expand Down
Loading