Skip to content

Commit

Permalink
Revision command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gururajsh committed Dec 17, 2024
1 parent 166079a commit 4fc9cb5
Show file tree
Hide file tree
Showing 22 changed files with 1,556 additions and 13 deletions.
23 changes: 23 additions & 0 deletions actor/v7action/annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v7action

import (
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/types"
)

func (actor *Actor) GetRevisionAnnotations(revisionGUID string) (map[string]types.NullString, Warnings, error) {
resource, warnings, err := actor.GetRevisionByGUID(revisionGUID)
return actor.extractAnnotations(resource.Metadata, warnings, err)
}

func (actor *Actor) extractAnnotations(metadata *resources.Metadata, warnings Warnings, err error) (map[string]types.NullString, Warnings, error) {
var annotations map[string]types.NullString

if err != nil {
return annotations, warnings, err
}
if metadata != nil {
annotations = metadata.Annotations
}
return annotations, warnings, nil
}
101 changes: 101 additions & 0 deletions actor/v7action/annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package v7action_test

import (
"errors"

. "code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("annotations", func() {
var (
actor *Actor
fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
fakeSharedActor *v7actionfakes.FakeSharedActor
fakeConfig *v7actionfakes.FakeConfig
warnings Warnings
executeErr error
annotations map[string]types.NullString
)

BeforeEach(func() {
fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
fakeSharedActor = new(v7actionfakes.FakeSharedActor)
fakeConfig = new(v7actionfakes.FakeConfig)
actor = NewActor(fakeCloudControllerClient, fakeConfig, fakeSharedActor, nil, nil, nil)
})

Describe("GetRevisionAnnotations", func() {
JustBeforeEach(func() {
annotations, warnings, executeErr = actor.GetRevisionAnnotations("some-guid")
})

When("there are no client errors", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{GUID: "some-guid"},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
nil,
)
})

It("gets the revision", func() {
Expect(fakeCloudControllerClient.GetRevisionCallCount()).To(Equal(1))
Expect(fakeCloudControllerClient.GetRevisionArgsForCall(0)).To(Equal("some-guid"))
})

When("there are no annotations on a revision", func() {
It("returns an empty map", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(annotations).To(BeEmpty())
})
})

When("there are annotations", func() {
var expectedAnnotations map[string]types.NullString

BeforeEach(func() {
expectedAnnotations = map[string]types.NullString{"key1": types.NewNullString("value1"), "key2": types.NewNullString("value2")}
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{
GUID: "some-guid",
Metadata: &resources.Metadata{
Annotations: expectedAnnotations,
},
},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
nil,
)
})
It("returns the annotations", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(annotations).To(Equal(expectedAnnotations))
})
})
})

When("there is a client error", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{GUID: "some-guid"},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
errors.New("get-revision-error"),
)
})
When("GetRevision fails", func() {
It("returns the error and all warnings", func() {
Expect(executeErr).To(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(executeErr).To(MatchError("get-revision-error"))
})
})
})
})
})
2 changes: 2 additions & 0 deletions actor/v7action/cloud_controller_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type CloudControllerClient interface {
GetDroplet(guid string) (resources.Droplet, ccv3.Warnings, error)
GetDroplets(query ...ccv3.Query) ([]resources.Droplet, ccv3.Warnings, error)
GetEnvironmentVariableGroup(group constant.EnvironmentVariableGroupName) (resources.EnvironmentVariables, ccv3.Warnings, error)
GetEnvironmentVariablesByURL(url string) (resources.EnvironmentVariables, ccv3.Warnings, error)
GetEvents(query ...ccv3.Query) ([]ccv3.Event, ccv3.Warnings, error)
GetFeatureFlag(featureFlagName string) (resources.FeatureFlag, ccv3.Warnings, error)
GetFeatureFlags() ([]resources.FeatureFlag, ccv3.Warnings, error)
Expand All @@ -105,6 +106,7 @@ type CloudControllerClient interface {
GetProcesses(query ...ccv3.Query) ([]resources.Process, ccv3.Warnings, error)
GetProcessInstances(processGUID string) ([]ccv3.ProcessInstance, ccv3.Warnings, error)
GetProcessSidecars(processGUID string) ([]resources.Sidecar, ccv3.Warnings, error)
GetRevision(revisionGUID string) (resources.Revision, ccv3.Warnings, error)
GetRoles(query ...ccv3.Query) ([]resources.Role, ccv3.IncludedResources, ccv3.Warnings, error)
GetRouteBindings(query ...ccv3.Query) ([]resources.RouteBinding, ccv3.IncludedResources, ccv3.Warnings, error)
GetRouteDestinations(routeGUID string) ([]resources.RouteDestination, ccv3.Warnings, error)
Expand Down
5 changes: 5 additions & 0 deletions actor/v7action/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (actor *Actor) GetBuildpackLabels(buildpackName string, buildpackStack stri
return actor.extractLabels(resource.Metadata, warnings, err)
}

func (actor *Actor) GetRevisionLabels(revisionGUID string) (map[string]types.NullString, Warnings, error) {
resource, warnings, err := actor.GetRevisionByGUID(revisionGUID)
return actor.extractLabels(resource.Metadata, warnings, err)
}

func (actor *Actor) extractLabels(metadata *resources.Metadata, warnings Warnings, err error) (map[string]types.NullString, Warnings, error) {
var labels map[string]types.NullString

Expand Down
69 changes: 69 additions & 0 deletions actor/v7action/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,4 +1470,73 @@ var _ = Describe("labels", func() {
})
})
})

Describe("GetRevisionLabels", func() {
JustBeforeEach(func() {
labels, warnings, executeErr = actor.GetRevisionLabels("some-guid")
})

When("there are no client errors", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{GUID: "some-guid"},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
nil,
)
})

It("gets the revision", func() {
Expect(fakeCloudControllerClient.GetRevisionCallCount()).To(Equal(1))
Expect(fakeCloudControllerClient.GetRevisionArgsForCall(0)).To(Equal("some-guid"))
})

When("there are no labels on a revision", func() {
It("returns an empty map", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(labels).To(BeEmpty())
})
})

When("there are labels", func() {
var expectedLabels map[string]types.NullString

BeforeEach(func() {
expectedLabels = map[string]types.NullString{"key1": types.NewNullString("value1"), "key2": types.NewNullString("value2")}
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{
GUID: "some-guid",
Metadata: &resources.Metadata{
Labels: expectedLabels,
},
},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
nil,
)
})
It("returns the labels", func() {
Expect(executeErr).NotTo(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(labels).To(Equal(expectedLabels))
})
})
})

When("there is a client error", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{GUID: "some-guid"},
ccv3.Warnings([]string{"warning-1", "warning-2"}),
errors.New("get-revision-error"),
)
})
When("GetRevision fails", func() {
It("returns the error and all warnings", func() {
Expect(executeErr).To(HaveOccurred())
Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
Expect(executeErr).To(MatchError("get-revision-error"))
})
})
})
})
})
12 changes: 12 additions & 0 deletions actor/v7action/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v7action

import "code.cloudfoundry.org/cli/resources"

func (actor Actor) GetRevisionByGUID(revisionGUID string) (resources.Revision, Warnings, error) {
revision, warnings, err := actor.CloudControllerClient.GetRevision(revisionGUID)
if err != nil {
return resources.Revision{}, Warnings(warnings), err
}

return resources.Revision(revision), Warnings(warnings), err
}
72 changes: 72 additions & 0 deletions actor/v7action/revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package v7action_test

import (
"errors"

. "code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Revision Actions", func() {
Describe("GetRevisionByGUID", func() {
var (
actor *Actor
fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
fakeConfig *v7actionfakes.FakeConfig
revisionGUID string
fetchedRevision resources.Revision
executeErr error
warnings Warnings
)

BeforeEach(func() {
fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
fakeConfig = new(v7actionfakes.FakeConfig)
actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil, nil, nil)
revisionGUID = "revision-guid"
fakeConfig.APIVersionReturns("3.86.0")
})

JustBeforeEach(func() {
fetchedRevision, warnings, executeErr = actor.GetRevisionByGUID(revisionGUID)
})

When("finding the revision fails", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{},
ccv3.Warnings{"get-revision-warning"},
errors.New("get-revision-error"),
)
})

It("returns an executeError", func() {
Expect(executeErr).To(MatchError("get-revision-error"))
Expect(warnings).To(ConsistOf("get-revision-warning"))
})
})

When("finding the revision succeeds", func() {
BeforeEach(func() {
fakeCloudControllerClient.GetRevisionReturns(
resources.Revision{GUID: "1", Deployable: true, Droplet: resources.Droplet{GUID: "droplet-guid-1"}},
nil,
nil,
)
})

It("returns the revision", func() {
Expect(fakeCloudControllerClient.GetRevisionCallCount()).To(Equal(1), "GetRevision call count")
Expect(fakeCloudControllerClient.GetRevisionArgsForCall(0)).To(Equal(revisionGUID))

Expect(fetchedRevision).To(Equal(
resources.Revision{GUID: "1", Deployable: true, Droplet: resources.Droplet{GUID: "droplet-guid-1"}}))
Expect(executeErr).ToNot(HaveOccurred())
})
})
})
})
14 changes: 14 additions & 0 deletions actor/v7action/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ func (actor Actor) GetApplicationRevisionsDeployed(appGUID string) ([]resources.

return revisions, Warnings(warnings), nil
}

func (actor Actor) GetEnvironmentVariableGroupByRevision(revision resources.Revision) (EnvironmentVariableGroup, bool, Warnings, error) {
envVarApiLink, isPresent := revision.Links["environment_variables"]
if !isPresent {
return EnvironmentVariableGroup{}, isPresent, Warnings{"Unable to retrieve environment variables for revision."}, nil
}

environmentVariables, warnings, err := actor.CloudControllerClient.GetEnvironmentVariablesByURL(envVarApiLink.HREF)
if err != nil {
return EnvironmentVariableGroup{}, false, Warnings(warnings), err
}

return EnvironmentVariableGroup(environmentVariables), true, Warnings(warnings), nil
}
Loading

0 comments on commit 4fc9cb5

Please sign in to comment.