Skip to content

Commit

Permalink
Combine applicationoffers and remoteendpoints facades onto a single c…
Browse files Browse the repository at this point in the history
…ontroller facade
  • Loading branch information
wallyworld committed May 10, 2017
1 parent d0ffa6a commit aa6835a
Show file tree
Hide file tree
Showing 34 changed files with 1,445 additions and 1,579 deletions.
10 changes: 5 additions & 5 deletions api/applicationoffers/access_test.go
Expand Up @@ -23,7 +23,7 @@ type accessFunc func(string, string, ...string) error
var _ = gc.Suite(&accessSuite{})

const (
someOffer = "hosted-mysql"
someOffer = "user/prod.hosted-mysql"
)

func accessCall(client *applicationoffers.Client, action params.OfferAction, user, access string, offerURLs ...string) error {
Expand Down Expand Up @@ -72,7 +72,7 @@ func (s *accessSuite) readOnlyUser(c *gc.C, action params.OfferAction) {
c.Assert(req.Changes, gc.HasLen, 1)
c.Assert(string(req.Changes[0].Action), gc.Equals, string(action))
c.Assert(string(req.Changes[0].Access), gc.Equals, string(params.OfferReadAccess))
c.Assert(req.Changes[0].OfferTag, gc.Equals, names.NewApplicationOfferTag(someOffer).String())
c.Assert(req.Changes[0].OfferURL, gc.Equals, someOffer)

resp := assertResponse(c, result)
*resp = params.ErrorResults{Results: []params.ErrorResult{{Error: nil}}}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (s *accessSuite) adminUser(c *gc.C, action params.OfferAction) {
c.Assert(req.Changes, gc.HasLen, 1)
c.Assert(string(req.Changes[0].Action), gc.Equals, string(action))
c.Assert(string(req.Changes[0].Access), gc.Equals, string(params.OfferConsumeAccess))
c.Assert(req.Changes[0].OfferTag, gc.Equals, names.NewApplicationOfferTag(someOffer).String())
c.Assert(req.Changes[0].OfferURL, gc.Equals, someOffer)

resp := assertResponse(c, result)
*resp = params.ErrorResults{Results: []params.ErrorResult{{Error: nil}}}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *accessSuite) threeOffers(c *gc.C, action params.OfferAction) {
for i := range req.Changes {
c.Assert(string(req.Changes[i].Action), gc.Equals, string(action))
c.Assert(string(req.Changes[i].Access), gc.Equals, string(params.OfferReadAccess))
c.Assert(req.Changes[i].OfferTag, gc.Equals, names.NewApplicationOfferTag(someOffer).String())
c.Assert(req.Changes[i].OfferURL, gc.Equals, someOffer)
}

resp := assertResponse(c, result)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *accessSuite) errorResult(c *gc.C, action params.OfferAction) {
c.Assert(req.Changes, gc.HasLen, 1)
c.Assert(string(req.Changes[0].Action), gc.Equals, string(action))
c.Assert(req.Changes[0].UserTag, gc.Equals, names.NewUserTag("aaa").String())
c.Assert(req.Changes[0].OfferTag, gc.Equals, names.NewApplicationOfferTag(someOffer).String())
c.Assert(req.Changes[0].OfferURL, gc.Equals, someOffer)

resp := assertResponse(c, result)
err := &params.Error{Message: "unfortunate mishap"}
Expand Down
80 changes: 71 additions & 9 deletions api/applicationoffers/client.go
Expand Up @@ -30,14 +30,15 @@ func NewClient(st base.APICallCloser) *Client {
}

// Offer prepares application's endpoints for consumption.
func (c *Client) Offer(application string, endpoints []string, offerName string, desc string) ([]params.ErrorResult, error) {
func (c *Client) Offer(modelUUID, application string, endpoints []string, offerName string, desc string) ([]params.ErrorResult, error) {
// TODO(wallyworld) - support endpoint aliases
ep := make(map[string]string)
for _, name := range endpoints {
ep[name] = name
}
offers := []params.AddApplicationOffer{
{
ModelTag: names.NewModelTag(modelUUID).String(),
ApplicationName: application,
ApplicationDescription: desc,
Endpoints: ep,
Expand All @@ -58,6 +59,7 @@ func (c *Client) ListOffers(filters ...crossmodel.ApplicationOfferFilter) ([]cro
for _, f := range filters {
// TODO(wallyworld) - include allowed users
filterTerm := params.OfferFilter{
ModelName: f.ModelName,
OfferName: f.OfferName,
ApplicationName: f.ApplicationName,
}
Expand Down Expand Up @@ -104,16 +106,16 @@ func convertListResultsToModel(items []params.ApplicationOfferDetails) []crossmo
}

// GrantOffer grants a user access to the specified offers.
func (c *Client) GrantOffer(user, access string, offers ...string) error {
return c.modifyOfferUser(params.GrantOfferAccess, user, access, offers)
func (c *Client) GrantOffer(user, access string, offerURLs ...string) error {
return c.modifyOfferUser(params.GrantOfferAccess, user, access, offerURLs)
}

// RevokeOffer revokes a user's access to the specified offers.
func (c *Client) RevokeOffer(user, access string, offers ...string) error {
return c.modifyOfferUser(params.RevokeOfferAccess, user, access, offers)
func (c *Client) RevokeOffer(user, access string, offerURLs ...string) error {
return c.modifyOfferUser(params.RevokeOfferAccess, user, access, offerURLs)
}

func (c *Client) modifyOfferUser(action params.OfferAction, user, access string, offers []string) error {
func (c *Client) modifyOfferUser(action params.OfferAction, user, access string, offerURLs []string) error {
var args params.ModifyOfferAccessRequest

if !names.IsValidUser(user) {
Expand All @@ -125,12 +127,12 @@ func (c *Client) modifyOfferUser(action params.OfferAction, user, access string,
if err := permission.ValidateOfferAccess(offerAccess); err != nil {
return errors.Trace(err)
}
for _, offer := range offers {
for _, offerURL := range offerURLs {
args.Changes = append(args.Changes, params.ModifyOfferAccess{
UserTag: userTag.String(),
Action: action,
Access: params.OfferAccessPermission(offerAccess),
OfferTag: names.NewApplicationOfferTag(offer).String(),
OfferURL: offerURL,
})
}

Expand All @@ -145,9 +147,69 @@ func (c *Client) modifyOfferUser(action params.OfferAction, user, access string,

for i, r := range result.Results {
if r.Error != nil && r.Error.Code == params.CodeAlreadyExists {
logger.Warningf("offer %q is already shared with %q", offers[i], userTag.Id())
logger.Warningf("offer %q is already shared with %q", offerURLs[i], userTag.Id())
result.Results[i].Error = nil
}
}
return result.Combine()
}

// ApplicationOffer returns offered remote application details for a given URL.
func (c *Client) ApplicationOffer(urlStr string) (params.ApplicationOffer, error) {

url, err := crossmodel.ParseApplicationURL(urlStr)
if err != nil {
return params.ApplicationOffer{}, errors.Trace(err)
}
if url.Source != "" {
return params.ApplicationOffer{}, errors.NotSupportedf("query for non-local application offers")
}

found := params.ApplicationOffersResults{}

err = c.facade.FacadeCall("ApplicationOffers", params.ApplicationURLs{[]string{urlStr}}, &found)
if err != nil {
return params.ApplicationOffer{}, errors.Trace(err)
}

result := found.Results
if len(result) != 1 {
return params.ApplicationOffer{}, errors.Errorf("expected to find one result for url %q but found %d", url, len(result))
}

theOne := result[0]
if theOne.Error != nil {
return params.ApplicationOffer{}, errors.Trace(theOne.Error)
}
return theOne.Result, nil
}

// FindApplicationOffers returns all application offers matching the supplied filter.
func (c *Client) FindApplicationOffers(filters ...crossmodel.ApplicationOfferFilter) ([]params.ApplicationOffer, error) {
// We need at least one filter. The default filter will list all local applications.
if len(filters) == 0 {
return nil, errors.New("at least one filter must be specified")
}
var paramsFilter params.OfferFilters
for _, f := range filters {
filterTerm := params.OfferFilter{
OfferName: f.OfferName,
ModelName: f.ModelName,
OwnerName: f.OwnerName,
}
filterTerm.Endpoints = make([]params.EndpointFilterAttributes, len(f.Endpoints))
for i, ep := range f.Endpoints {
filterTerm.Endpoints[i].Name = ep.Name
filterTerm.Endpoints[i].Interface = ep.Interface
filterTerm.Endpoints[i].Role = ep.Role
}
paramsFilter.Filters = append(paramsFilter.Filters, filterTerm)
}

out := params.FindApplicationOffersResults{}
err := c.facade.FacadeCall("FindApplicationOffers", paramsFilter, &out)
if err != nil {
return nil, errors.Trace(err)
}
return out.Results, nil
}

0 comments on commit aa6835a

Please sign in to comment.