forked from juju/juju
/
uniter.go
988 lines (934 loc) · 29.6 KB
/
uniter.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The uniter package implements the API interface
// used by the uniter worker.
package uniter
import (
"fmt"
"github.com/juju/charm"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/apiserver/common"
"github.com/juju/juju/state/watcher"
)
// UniterAPI implements the API used by the uniter worker.
type UniterAPI struct {
*common.LifeGetter
*common.StatusSetter
*common.DeadEnsurer
*common.AgentEntityWatcher
*common.APIAddresser
*common.EnvironWatcher
st *state.State
auth common.Authorizer
resources *common.Resources
accessUnit common.GetAuthFunc
accessService common.GetAuthFunc
}
// NewUniterAPI creates a new instance of the Uniter API.
func NewUniterAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*UniterAPI, error) {
if !authorizer.AuthUnitAgent() {
return nil, common.ErrPerm
}
accessUnit := func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
}
accessService := func() (common.AuthFunc, error) {
unit, ok := authorizer.GetAuthEntity().(*state.Unit)
if !ok {
panic("authenticated entity is not a unit")
}
return func(tag string) bool {
return tag == names.NewServiceTag(unit.ServiceName()).String()
}, nil
}
accessUnitOrService := common.AuthEither(accessUnit, accessService)
// Uniter can always watch for environ changes.
getCanWatch := common.AuthAlways(true)
// Uniter can not get the secrets.
getCanReadSecrets := common.AuthAlways(false)
return &UniterAPI{
LifeGetter: common.NewLifeGetter(st, accessUnitOrService),
StatusSetter: common.NewStatusSetter(st, accessUnit),
DeadEnsurer: common.NewDeadEnsurer(st, accessUnit),
AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, accessUnitOrService),
APIAddresser: common.NewAPIAddresser(st, resources),
EnvironWatcher: common.NewEnvironWatcher(st, resources, getCanWatch, getCanReadSecrets),
st: st,
auth: authorizer,
resources: resources,
accessUnit: accessUnit,
accessService: accessService,
}, nil
}
func (u *UniterAPI) getUnit(tag string) (*state.Unit, error) {
t, err := names.ParseTag(tag, names.UnitTagKind)
if err != nil {
return nil, err
}
return u.st.Unit(t.Id())
}
func (u *UniterAPI) getService(tag string) (*state.Service, error) {
t, err := names.ParseTag(tag, names.ServiceTagKind)
if err != nil {
return nil, err
}
return u.st.Service(t.Id())
}
// PublicAddress returns the public address for each given unit, if set.
func (u *UniterAPI) PublicAddress(args params.Entities) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.StringResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
address, ok := unit.PublicAddress()
if ok {
result.Results[i].Result = address
} else {
err = common.NoAddressSetError(entity.Tag, "public")
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// PrivateAddress returns the private address for each given unit, if set.
func (u *UniterAPI) PrivateAddress(args params.Entities) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.StringResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
address, ok := unit.PrivateAddress()
if ok {
result.Results[i].Result = address
} else {
err = common.NoAddressSetError(entity.Tag, "private")
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// Resolved returns the current resolved setting for each given unit.
func (u *UniterAPI) Resolved(args params.Entities) (params.ResolvedModeResults, error) {
result := params.ResolvedModeResults{
Results: make([]params.ResolvedModeResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ResolvedModeResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
result.Results[i].Mode = params.ResolvedMode(unit.Resolved())
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// ClearResolved removes any resolved setting from each given unit.
func (u *UniterAPI) ClearResolved(args params.Entities) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
err = unit.ClearResolved()
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// GetPrincipal returns the result of calling PrincipalName() and
// converting it to a tag, on each given unit.
func (u *UniterAPI) GetPrincipal(args params.Entities) (params.StringBoolResults, error) {
result := params.StringBoolResults{
Results: make([]params.StringBoolResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.StringBoolResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
principal, ok := unit.PrincipalName()
if principal != "" {
result.Results[i].Result = names.NewUnitTag(principal).String()
}
result.Results[i].Ok = ok
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// Destroy advances all given Alive units' lifecycles as far as
// possible. See state/Unit.Destroy().
func (u *UniterAPI) Destroy(args params.Entities) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
err = unit.Destroy()
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) destroySubordinates(principal *state.Unit) error {
subordinates := principal.SubordinateNames()
for _, subName := range subordinates {
unit, err := u.getUnit(names.NewUnitTag(subName).String())
if err != nil {
return err
}
if err = unit.Destroy(); err != nil {
return err
}
}
return nil
}
// DestroyAllSubordinates destroys all subordinates of each given unit.
func (u *UniterAPI) DestroyAllSubordinates(args params.Entities) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
err = u.destroySubordinates(unit)
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// HasSubordinates returns the whether each given unit has any subordinates.
func (u *UniterAPI) HasSubordinates(args params.Entities) (params.BoolResults, error) {
result := params.BoolResults{
Results: make([]params.BoolResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.BoolResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
subordinates := unit.SubordinateNames()
result.Results[i].Result = len(subordinates) > 0
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// CharmURL returns the charm URL for all given units or services.
func (u *UniterAPI) CharmURL(args params.Entities) (params.StringBoolResults, error) {
result := params.StringBoolResults{
Results: make([]params.StringBoolResult, len(args.Entities)),
}
accessUnitOrService := common.AuthEither(u.accessUnit, u.accessService)
canAccess, err := accessUnitOrService()
if err != nil {
return params.StringBoolResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unitOrService state.Entity
unitOrService, err = u.st.FindEntity(entity.Tag)
if err == nil {
charmURLer := unitOrService.(interface {
CharmURL() (*charm.URL, bool)
})
curl, ok := charmURLer.CharmURL()
if curl != nil {
result.Results[i].Result = curl.String()
result.Results[i].Ok = ok
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// SetCharmURL sets the charm URL for each given unit. An error will
// be returned if a unit is dead, or the charm URL is not know.
func (u *UniterAPI) SetCharmURL(args params.EntitiesCharmURL) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
var curl *charm.URL
curl, err = charm.ParseURL(entity.CharmURL)
if err == nil {
err = unit.SetCharmURL(curl)
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// OpenPort sets the policy of the port with protocol an number to be
// opened, for all given units.
func (u *UniterAPI) OpenPort(args params.EntitiesPorts) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
err = unit.OpenPort(entity.Protocol, entity.Port)
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// ClosePort sets the policy of the port with protocol and number to
// be closed, for all given units.
func (u *UniterAPI) ClosePort(args params.EntitiesPorts) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
err = unit.ClosePort(entity.Protocol, entity.Port)
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) watchOneUnitConfigSettings(tag string) (string, error) {
unit, err := u.getUnit(tag)
if err != nil {
return "", err
}
watch, err := unit.WatchConfigSettings()
if err != nil {
return "", err
}
// Consume the initial event. Technically, API
// calls to Watch 'transmit' the initial event
// in the Watch response. But NotifyWatchers
// have no state to transmit.
if _, ok := <-watch.Changes(); ok {
return u.resources.Register(watch), nil
}
return "", watcher.MustErr(watch)
}
// WatchConfigSettings returns a NotifyWatcher for observing changes
// to each unit's service configuration settings. See also
// state/watcher.go:Unit.WatchConfigSettings().
func (u *UniterAPI) WatchConfigSettings(args params.Entities) (params.NotifyWatchResults, error) {
result := params.NotifyWatchResults{
Results: make([]params.NotifyWatchResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.NotifyWatchResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
watcherId := ""
if canAccess(entity.Tag) {
watcherId, err = u.watchOneUnitConfigSettings(entity.Tag)
}
result.Results[i].NotifyWatcherId = watcherId
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// ConfigSettings returns the complete set of service charm config
// settings available to each given unit.
func (u *UniterAPI) ConfigSettings(args params.Entities) (params.ConfigSettingsResults, error) {
result := params.ConfigSettingsResults{
Results: make([]params.ConfigSettingsResult, len(args.Entities)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ConfigSettingsResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
var settings charm.Settings
settings, err = unit.ConfigSettings()
if err == nil {
result.Results[i].Settings = params.ConfigSettings(settings)
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) watchOneServiceRelations(tag string) (params.StringsWatchResult, error) {
nothing := params.StringsWatchResult{}
service, err := u.getService(tag)
if err != nil {
return nothing, err
}
watch := service.WatchRelations()
// Consume the initial event and forward it to the result.
if changes, ok := <-watch.Changes(); ok {
return params.StringsWatchResult{
StringsWatcherId: u.resources.Register(watch),
Changes: changes,
}, nil
}
return nothing, watcher.MustErr(watch)
}
// WatchServiceRelations returns a StringsWatcher, for each given
// service, that notifies of changes to the lifecycles of relations
// involving that service.
func (u *UniterAPI) WatchServiceRelations(args params.Entities) (params.StringsWatchResults, error) {
result := params.StringsWatchResults{
Results: make([]params.StringsWatchResult, len(args.Entities)),
}
canAccess, err := u.accessService()
if err != nil {
return params.StringsWatchResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canAccess(entity.Tag) {
result.Results[i], err = u.watchOneServiceRelations(entity.Tag)
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// CharmArchiveURL returns the URL, corresponding to the charm archive
// (bundle) in the provider storage for each given charm URL, along
// with the DisableSSLHostnameVerification flag.
func (u *UniterAPI) CharmArchiveURL(args params.CharmURLs) (params.CharmArchiveURLResults, error) {
result := params.CharmArchiveURLResults{
Results: make([]params.CharmArchiveURLResult, len(args.URLs)),
}
// Get the SSL hostname verification environment setting.
envConfig, err := u.st.EnvironConfig()
if err != nil {
return result, err
}
// SSLHostnameVerification defaults to true, so we need to
// invert that, for backwards-compatibility (older versions
// will have DisableSSLHostnameVerification: false by default).
disableSSLHostnameVerification := !envConfig.SSLHostnameVerification()
for i, arg := range args.URLs {
curl, err := charm.ParseURL(arg.URL)
if err != nil {
err = common.ErrPerm
} else {
var sch *state.Charm
sch, err = u.st.Charm(curl)
if errors.IsNotFound(err) {
err = common.ErrPerm
}
if err == nil {
result.Results[i].Result = sch.BundleURL().String()
result.Results[i].DisableSSLHostnameVerification = disableSSLHostnameVerification
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// CharmArchiveSha256 returns the SHA256 digest of the charm archive
// (bundle) data for each charm url in the given parameters.
func (u *UniterAPI) CharmArchiveSha256(args params.CharmURLs) (params.StringResults, error) {
result := params.StringResults{
Results: make([]params.StringResult, len(args.URLs)),
}
for i, arg := range args.URLs {
curl, err := charm.ParseURL(arg.URL)
if err != nil {
err = common.ErrPerm
} else {
var sch *state.Charm
sch, err = u.st.Charm(curl)
if errors.IsNotFound(err) {
err = common.ErrPerm
}
if err == nil {
result.Results[i].Result = sch.BundleSha256()
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) getRelationAndUnit(canAccess common.AuthFunc, relTag, unitTag string) (*state.Relation, *state.Unit, error) {
tag, err := names.ParseTag(relTag, names.RelationTagKind)
if err != nil {
return nil, nil, common.ErrPerm
}
rel, err := u.st.KeyRelation(tag.Id())
if errors.IsNotFound(err) {
return nil, nil, common.ErrPerm
} else if err != nil {
return nil, nil, err
}
if !canAccess(unitTag) {
return nil, nil, common.ErrPerm
}
unit, err := u.getUnit(unitTag)
return rel, unit, err
}
func (u *UniterAPI) prepareRelationResult(rel *state.Relation, unit *state.Unit) (params.RelationResult, error) {
nothing := params.RelationResult{}
ep, err := rel.Endpoint(unit.ServiceName())
if err != nil {
// An error here means the unit's service is not part of the
// relation.
return nothing, err
}
return params.RelationResult{
Id: rel.Id(),
Key: rel.String(),
Life: params.Life(rel.Life().String()),
Endpoint: params.Endpoint{
ServiceName: ep.ServiceName,
Relation: ep.Relation,
},
}, nil
}
func (u *UniterAPI) getOneRelation(canAccess common.AuthFunc, relTag, unitTag string) (params.RelationResult, error) {
nothing := params.RelationResult{}
rel, unit, err := u.getRelationAndUnit(canAccess, relTag, unitTag)
if err != nil {
return nothing, err
}
return u.prepareRelationResult(rel, unit)
}
func (u *UniterAPI) getOneRelationById(relId int) (params.RelationResult, error) {
nothing := params.RelationResult{}
rel, err := u.st.Relation(relId)
if errors.IsNotFound(err) {
return nothing, common.ErrPerm
} else if err != nil {
return nothing, err
}
// Use the currently authenticated unit to get the endpoint.
unit, ok := u.auth.GetAuthEntity().(*state.Unit)
if !ok {
panic("authenticated entity is not a unit")
}
result, err := u.prepareRelationResult(rel, unit)
if err != nil {
// An error from prepareRelationResult means the authenticated
// unit's service is not part of the requested
// relation. That's why it's appropriate to return ErrPerm
// here.
return nothing, common.ErrPerm
}
return result, nil
}
func (u *UniterAPI) getRelationUnit(canAccess common.AuthFunc, relTag, unitTag string) (*state.RelationUnit, error) {
rel, unit, err := u.getRelationAndUnit(canAccess, relTag, unitTag)
if err != nil {
return nil, err
}
return rel.Unit(unit)
}
// Relation returns information about all given relation/unit pairs,
// including their id, key and the local endpoint.
func (u *UniterAPI) Relation(args params.RelationUnits) (params.RelationResults, error) {
result := params.RelationResults{
Results: make([]params.RelationResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.RelationResults{}, err
}
for i, rel := range args.RelationUnits {
relParams, err := u.getOneRelation(canAccess, rel.Relation, rel.Unit)
if err == nil {
result.Results[i] = relParams
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// RelationById returns information about all given relations,
// specified by their ids, including their key and the local
// endpoint.
func (u *UniterAPI) RelationById(args params.RelationIds) (params.RelationResults, error) {
result := params.RelationResults{
Results: make([]params.RelationResult, len(args.RelationIds)),
}
for i, relId := range args.RelationIds {
relParams, err := u.getOneRelationById(relId)
if err == nil {
result.Results[i] = relParams
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func relationsInScopeTags(unit *state.Unit) ([]string, error) {
relations, err := unit.RelationsInScope()
if err != nil {
return nil, err
}
tags := make([]string, len(relations))
for i, relation := range relations {
tags[i] = relation.Tag()
}
return tags, nil
}
// JoinedRelations returns the tags of all relations for which each supplied unit
// has entered scope. It should be called RelationsInScope, but it's not convenient
// to make that change until we have versioned APIs.
func (u *UniterAPI) JoinedRelations(args params.Entities) (params.StringsResults, error) {
result := params.StringsResults{
Results: make([]params.StringsResult, len(args.Entities)),
}
if len(args.Entities) == 0 {
return result, nil
}
canRead, err := u.accessUnit()
if err != nil {
return params.StringsResults{}, err
}
for i, entity := range args.Entities {
err := common.ErrPerm
if canRead(entity.Tag) {
var unit *state.Unit
unit, err = u.getUnit(entity.Tag)
if err == nil {
result.Results[i].Result, err = relationsInScopeTags(unit)
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// CurrentEnvironUUID returns the UUID for the current juju environment.
func (u *UniterAPI) CurrentEnvironUUID() (params.StringResult, error) {
result := params.StringResult{}
env, err := u.st.Environment()
if err == nil {
result.Result = env.UUID()
}
return result, err
}
// CurrentEnvironment returns the name and UUID for the current juju environment.
func (u *UniterAPI) CurrentEnvironment() (params.EnvironmentResult, error) {
result := params.EnvironmentResult{}
env, err := u.st.Environment()
if err == nil {
result.Name = env.Name()
result.UUID = env.UUID()
}
return result, err
}
// ProviderType returns the provider type used by the current juju
// environment.
//
// TODO(dimitern): Refactor the uniter to call this instead of calling
// EnvironConfig() just to get the provider type. Once we have machine
// addresses, this might be completely unnecessary though.
func (u *UniterAPI) ProviderType() (params.StringResult, error) {
result := params.StringResult{}
cfg, err := u.st.EnvironConfig()
if err == nil {
result.Result = cfg.Type()
}
return result, err
}
// EnterScope ensures each unit has entered its scope in the relation,
// for all of the given relation/unit pairs. See also
// state.RelationUnit.EnterScope().
func (u *UniterAPI) EnterScope(args params.RelationUnits) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, arg := range args.RelationUnits {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.Unit)
if err == nil {
// Construct the settings, passing the unit's
// private address (we already know it).
privateAddress, _ := relUnit.PrivateAddress()
settings := map[string]interface{}{
"private-address": privateAddress,
}
err = relUnit.EnterScope(settings)
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// LeaveScope signals each unit has left its scope in the relation,
// for all of the given relation/unit pairs. See also
// state.RelationUnit.LeaveScope().
func (u *UniterAPI) LeaveScope(args params.RelationUnits) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, arg := range args.RelationUnits {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.Unit)
if err == nil {
err = relUnit.LeaveScope()
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func convertRelationSettings(settings map[string]interface{}) (params.RelationSettings, error) {
result := make(params.RelationSettings)
for k, v := range settings {
// All relation settings should be strings.
sval, ok := v.(string)
if !ok {
return nil, fmt.Errorf("unexpected relation setting %q: expected string, got %T", k, v)
}
result[k] = sval
}
return result, nil
}
// ReadSettings returns the local settings of each given set of
// relation/unit.
func (u *UniterAPI) ReadSettings(args params.RelationUnits) (params.RelationSettingsResults, error) {
result := params.RelationSettingsResults{
Results: make([]params.RelationSettingsResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.RelationSettingsResults{}, err
}
for i, arg := range args.RelationUnits {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.Unit)
if err == nil {
var settings *state.Settings
settings, err = relUnit.Settings()
if err == nil {
result.Results[i].Settings, err = convertRelationSettings(settings.Map())
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) checkRemoteUnit(relUnit *state.RelationUnit, remoteUnitTag string) (string, error) {
// Make sure the unit is indeed remote.
if remoteUnitTag == u.auth.GetAuthTag() {
return "", common.ErrPerm
}
// Check remoteUnit is indeed related. Note that we don't want to actually get
// the *Unit, because it might have been removed; but its relation settings will
// persist until the relation itself has been removed (and must remain accessible
// because the local unit's view of reality may be time-shifted).
tag, err := names.ParseTag(remoteUnitTag, names.UnitTagKind)
if err != nil {
return "", err
}
remoteUnitName := tag.Id()
remoteServiceName := names.UnitService(remoteUnitName)
rel := relUnit.Relation()
_, err = rel.RelatedEndpoints(remoteServiceName)
if err != nil {
return "", common.ErrPerm
}
return remoteUnitName, nil
}
// ReadRemoteSettings returns the remote settings of each given set of
// relation/local unit/remote unit.
func (u *UniterAPI) ReadRemoteSettings(args params.RelationUnitPairs) (params.RelationSettingsResults, error) {
result := params.RelationSettingsResults{
Results: make([]params.RelationSettingsResult, len(args.RelationUnitPairs)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.RelationSettingsResults{}, err
}
for i, arg := range args.RelationUnitPairs {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.LocalUnit)
if err == nil {
remoteUnit := ""
remoteUnit, err = u.checkRemoteUnit(relUnit, arg.RemoteUnit)
if err == nil {
var settings map[string]interface{}
settings, err = relUnit.ReadSettings(remoteUnit)
if err == nil {
result.Results[i].Settings, err = convertRelationSettings(settings)
}
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// UpdateSettings persists all changes made to the local settings of
// all given pairs of relation and unit. Keys with empty values are
// considered a signal to delete these values.
func (u *UniterAPI) UpdateSettings(args params.RelationUnitsSettings) (params.ErrorResults, error) {
result := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.ErrorResults{}, err
}
for i, arg := range args.RelationUnits {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.Unit)
if err == nil {
var settings *state.Settings
settings, err = relUnit.Settings()
if err == nil {
for k, v := range arg.Settings {
if v == "" {
settings.Delete(k)
} else {
settings.Set(k, v)
}
}
_, err = settings.Write()
}
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
func (u *UniterAPI) watchOneRelationUnit(relUnit *state.RelationUnit) (params.RelationUnitsWatchResult, error) {
watch := relUnit.Watch()
// Consume the initial event and forward it to the result.
if changes, ok := <-watch.Changes(); ok {
return params.RelationUnitsWatchResult{
RelationUnitsWatcherId: u.resources.Register(watch),
Changes: changes,
}, nil
}
return params.RelationUnitsWatchResult{}, watcher.MustErr(watch)
}
// WatchRelationUnits returns a RelationUnitsWatcher for observing
// changes to every unit in the supplied relation that is visible to
// the supplied unit. See also state/watcher.go:RelationUnit.Watch().
func (u *UniterAPI) WatchRelationUnits(args params.RelationUnits) (params.RelationUnitsWatchResults, error) {
result := params.RelationUnitsWatchResults{
Results: make([]params.RelationUnitsWatchResult, len(args.RelationUnits)),
}
canAccess, err := u.accessUnit()
if err != nil {
return params.RelationUnitsWatchResults{}, err
}
for i, arg := range args.RelationUnits {
relUnit, err := u.getRelationUnit(canAccess, arg.Relation, arg.Unit)
if err == nil {
result.Results[i], err = u.watchOneRelationUnit(relUnit)
}
result.Results[i].Error = common.ServerError(err)
}
return result, nil
}
// TODO(dimitern) bug #1270795 2014-01-20
// Add a doc comment here and use u.accessService()
// below in the body to check for permissions.
func (u *UniterAPI) GetOwnerTag(args params.Entities) (params.StringResult, error) {
nothing := params.StringResult{}
service, err := u.getService(args.Entities[0].Tag)
if err != nil {
return nothing, err
}
return params.StringResult{
Result: service.GetOwnerTag(),
}, nil
}