-
Notifications
You must be signed in to change notification settings - Fork 491
/
consume.go
196 lines (174 loc) · 5.42 KB
/
consume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/client/application"
"github.com/juju/juju/api/client/applicationoffers"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/crossmodel"
"github.com/juju/juju/rpc/params"
)
var usageConsumeSummary = `
Add a remote offer to the model.`[1:]
var usageConsumeDetails = `
Adds a remote offer to the model. Relations can be created later using "juju relate".
The path to the remote offer is formatted as follows:
[<controller name>:][<model owner>/]<model name>.<application name>
If the controller name is omitted, Juju will use the currently active
controller. Similarly, if the model owner is omitted, Juju will use the user
that is currently logged in to the controller providing the offer.
`[1:]
const usageConsumeExamples = `
juju consume othermodel.mysql
juju consume owner/othermodel.mysql
juju consume anothercontroller:owner/othermodel.mysql
`
// NewConsumeCommand returns a command to add remote offers to
// the model.
func NewConsumeCommand() cmd.Command {
return modelcmd.Wrap(&consumeCommand{})
}
// consumeCommand adds remote offers to the model without
// relating them to other applications.
type consumeCommand struct {
modelcmd.ModelCommandBase
sourceAPI applicationConsumeDetailsAPI
targetAPI applicationConsumeAPI
remoteApplication string
applicationAlias string
}
// Info implements cmd.Command.
func (c *consumeCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "consume",
Args: "<remote offer path> [<local application name>]",
Purpose: usageConsumeSummary,
Doc: usageConsumeDetails,
Examples: usageConsumeExamples,
SeeAlso: []string{
"integrate",
"offer",
"remove-saas",
},
})
}
// Init implements cmd.Command.
func (c *consumeCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("no remote offer specified")
}
c.remoteApplication = args[0]
if len(args) > 1 {
if !names.IsValidApplication(args[1]) {
return errors.Errorf("invalid application name %q", args[1])
}
c.applicationAlias = args[1]
return cmd.CheckEmpty(args[2:])
}
return nil
}
func (c *consumeCommand) getTargetAPI() (applicationConsumeAPI, error) {
if c.targetAPI != nil {
return c.targetAPI, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return application.NewClient(root), nil
}
func (c *consumeCommand) getSourceAPI(url *crossmodel.OfferURL) (applicationConsumeDetailsAPI, error) {
if c.sourceAPI != nil {
return c.sourceAPI, nil
}
if url.Source == "" {
var err error
controllerName, err := c.ControllerName()
if err != nil {
return nil, errors.Trace(err)
}
url.Source = controllerName
}
root, err := c.CommandBase.NewAPIRoot(c.ClientStore(), url.Source, "")
if err != nil {
return nil, errors.Trace(err)
}
return applicationoffers.NewClient(root), nil
}
// Run adds the requested remote offer to the model. Implements
// cmd.Command.
func (c *consumeCommand) Run(ctx *cmd.Context) error {
accountDetails, err := c.CurrentAccountDetails()
if err != nil {
return errors.Trace(err)
}
url, err := crossmodel.ParseOfferURL(c.remoteApplication)
if err != nil {
return errors.Trace(err)
}
if url.HasEndpoint() {
return errors.Errorf("saas offer %q shouldn't include endpoint", c.remoteApplication)
}
if url.User == "" {
url.User = accountDetails.User
c.remoteApplication = url.Path()
}
sourceClient, err := c.getSourceAPI(url)
if err != nil {
return errors.Trace(err)
}
defer sourceClient.Close()
consumeDetails, err := sourceClient.GetConsumeDetails(url.AsLocal().String())
if err != nil {
return errors.Trace(err)
}
// Parse the offer details URL and add the source controller so
// things like status can show the original source of the offer.
offerURL, err := crossmodel.ParseOfferURL(consumeDetails.Offer.OfferURL)
if err != nil {
return errors.Trace(err)
}
offerURL.Source = url.Source
consumeDetails.Offer.OfferURL = offerURL.String()
targetClient, err := c.getTargetAPI()
if err != nil {
return errors.Trace(err)
}
defer targetClient.Close()
arg := crossmodel.ConsumeApplicationArgs{
Offer: *consumeDetails.Offer,
ApplicationAlias: c.applicationAlias,
Macaroon: consumeDetails.Macaroon,
}
if consumeDetails.ControllerInfo != nil {
controllerTag, err := names.ParseControllerTag(consumeDetails.ControllerInfo.ControllerTag)
if err != nil {
return errors.Trace(err)
}
arg.ControllerInfo = &crossmodel.ControllerInfo{
ControllerTag: controllerTag,
Alias: consumeDetails.ControllerInfo.Alias,
Addrs: consumeDetails.ControllerInfo.Addrs,
CACert: consumeDetails.ControllerInfo.CACert,
}
}
localName, err := targetClient.Consume(arg)
if err != nil {
return block.ProcessBlockedError(errors.Annotatef(err, "could not consume %v", url.AsLocal().String()), block.BlockChange)
}
ctx.Infof("Added %s as %s", c.remoteApplication, localName)
return nil
}
type applicationConsumeAPI interface {
Close() error
Consume(crossmodel.ConsumeApplicationArgs) (string, error)
}
type applicationConsumeDetailsAPI interface {
Close() error
GetConsumeDetails(string) (params.ConsumeOfferDetails, error)
}