forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
1165 lines (997 loc) · 34 KB
/
application.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
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"encoding/base64"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/schema"
)
// Application represents a deployed charm in a model.
type Application interface {
HasAnnotations
HasConstraints
HasOperatorStatus
HasStatus
HasStatusHistory
Tag() names.ApplicationTag
Name() string
Type() string
Subordinate() bool
CharmURL() string
Channel() string
CharmModifiedVersion() int
ForceCharm() bool
MinUnits() int
Exposed() bool
ExposedEndpoints() map[string]ExposedEndpoint
PasswordHash() string
PodSpec() string
DesiredScale() int
Placement() string
HasResources() bool
CloudService() CloudService
SetCloudService(CloudServiceArgs)
EndpointBindings() map[string]string
CharmConfig() map[string]interface{}
ApplicationConfig() map[string]interface{}
Leader() string
LeadershipSettings() map[string]interface{}
MetricsCredentials() []byte
StorageDirectives() map[string]StorageDirective
Resources() []Resource
AddResource(ResourceArgs) Resource
Units() []Unit
AddUnit(UnitArgs) Unit
CharmOrigin() CharmOrigin
SetCharmOrigin(CharmOriginArgs)
CharmMetadata() CharmMetadata
SetCharmMetadata(CharmMetadataArgs)
CharmManifest() CharmManifest
SetCharmManifest(CharmManifestArgs)
CharmActions() CharmActions
SetCharmActions(CharmActionsArgs)
CharmConfigs() CharmConfigs
SetCharmConfigs(CharmConfigsArgs)
Tools() AgentTools
SetTools(AgentToolsArgs)
Offers() []ApplicationOffer
AddOffer(ApplicationOfferArgs) ApplicationOffer
Validate() error
OpenedPortRanges() PortRanges
AddOpenedPortRange(OpenedPortRangeArgs)
ProvisioningState() ProvisioningState
}
// ExposedEndpoint encapsulates the details about the CIDRs and/or spaces that
// should be able to access ports opened by the application for a particular
// endpoint once the application is exposed.
type ExposedEndpoint interface {
ExposeToSpaceIDs() []string
ExposeToCIDRs() []string
}
type applications struct {
Version int `yaml:"version"`
Applications_ []*application `yaml:"applications"`
}
type application struct {
Name_ string `yaml:"name"`
Type_ string `yaml:"type"`
// Series obsolete from v9. Retained for tests.
Series_ string `yaml:"series,omitempty"`
Subordinate_ bool `yaml:"subordinate,omitempty"`
CharmURL_ string `yaml:"charm-url"`
Channel_ string `yaml:"cs-channel"`
CharmModifiedVersion_ int `yaml:"charm-mod-version"`
// ForceCharm is true if an upgrade charm is forced.
// It means upgrade even if the charm is in an error state.
ForceCharm_ bool `yaml:"force-charm,omitempty"`
MinUnits_ int `yaml:"min-units,omitempty"`
Exposed_ bool `yaml:"exposed,omitempty"`
ExposedEndpoints_ map[string]*exposedEndpoint `yaml:"exposed-endpoints,omitempty"`
Status_ *status `yaml:"status"`
StatusHistory_ `yaml:"status-history"`
EndpointBindings_ map[string]string `yaml:"endpoint-bindings,omitempty"`
// CharmConfig_ and ApplicationConfig_ are the actual configuration values
// for the charm and application, respectively. These are the values that
// have been set by the user or the charm itself.
CharmConfig_ map[string]interface{} `yaml:"settings"`
ApplicationConfig_ map[string]interface{} `yaml:"application-config,omitempty"`
Leader_ string `yaml:"leader,omitempty"`
LeadershipSettings_ map[string]interface{} `yaml:"leadership-settings"`
MetricsCredentials_ string `yaml:"metrics-creds,omitempty"`
// unit count will be assumed by the number of units associated.
Units_ units `yaml:"units"`
Resources_ resources `yaml:"resources"`
Annotations_ `yaml:"annotations,omitempty"`
Constraints_ *constraints `yaml:"constraints,omitempty"`
StorageDirectives_ map[string]*storageDirective `yaml:"storage-directives,omitempty"`
// CAAS application fields.
PasswordHash_ string `yaml:"password-hash,omitempty"`
PodSpec_ string `yaml:"pod-spec,omitempty"`
Placement_ string `yaml:"placement,omitempty"`
HasResources_ bool `yaml:"has-resources,omitempty"`
DesiredScale_ int `yaml:"desired-scale,omitempty"`
CloudService_ *cloudService `yaml:"cloud-service,omitempty"`
Tools_ *agentTools `yaml:"tools,omitempty"`
OperatorStatus_ *status `yaml:"operator-status,omitempty"`
ProvisioningState_ *provisioningState `yaml:"provisioning-state,omitempty"`
OpenedPortRanges_ *deployedPortRanges `yaml:"opened-port-ranges,omitempty"`
// Offer-related fields
Offers_ *applicationOffers `yaml:"offers,omitempty"`
// CharmOrigin fields
CharmOrigin_ *charmOrigin `yaml:"charm-origin,omitempty"`
// The following fields represent the actual charm data for the
// application. These are the immutable parts of the application, either
// provided by the charm itself.
CharmMetadata_ *charmMetadata `yaml:"charm-metadata,omitempty"`
CharmManifest_ *charmManifest `yaml:"charm-manifest,omitempty"`
CharmActions_ *charmActions `yaml:"charm-actions,omitempty"`
CharmConfigs_ *charmConfigs `yaml:"charm-configs,omitempty"`
}
// ApplicationArgs is an argument struct used to add an application to the Model.
type ApplicationArgs struct {
Tag names.ApplicationTag
Type string
// Series obsolete from v9. Retained for tests.
Series string
Subordinate bool
CharmURL string
Channel string
CharmModifiedVersion int
ForceCharm bool
PasswordHash string
PodSpec string
Placement string
HasResources bool
DesiredScale int
CloudService *CloudServiceArgs
MinUnits int
Exposed bool
ExposedEndpoints map[string]ExposedEndpointArgs
EndpointBindings map[string]string
ApplicationConfig map[string]interface{}
CharmConfig map[string]interface{}
Leader string
LeadershipSettings map[string]interface{}
StorageDirectives map[string]StorageDirectiveArgs
MetricsCredentials []byte
ProvisioningState *ProvisioningStateArgs
}
func newApplication(args ApplicationArgs) *application {
creds := base64.StdEncoding.EncodeToString(args.MetricsCredentials)
app := &application{
Name_: args.Tag.Id(),
Type_: args.Type,
Series_: args.Series,
Subordinate_: args.Subordinate,
CharmURL_: args.CharmURL,
Channel_: args.Channel,
CharmModifiedVersion_: args.CharmModifiedVersion,
ForceCharm_: args.ForceCharm,
Exposed_: args.Exposed,
PasswordHash_: args.PasswordHash,
PodSpec_: args.PodSpec,
CloudService_: newCloudService(args.CloudService),
Placement_: args.Placement,
HasResources_: args.HasResources,
DesiredScale_: args.DesiredScale,
MinUnits_: args.MinUnits,
EndpointBindings_: args.EndpointBindings,
ApplicationConfig_: args.ApplicationConfig,
CharmConfig_: args.CharmConfig,
Leader_: args.Leader,
LeadershipSettings_: args.LeadershipSettings,
MetricsCredentials_: creds,
StatusHistory_: newStatusHistory(),
ProvisioningState_: newProvisioningState(args.ProvisioningState),
}
app.setUnits(nil)
app.setResources(nil)
if len(args.StorageDirectives) > 0 {
app.StorageDirectives_ = make(map[string]*storageDirective)
for key, value := range args.StorageDirectives {
app.StorageDirectives_[key] = newStorageDirective(value)
}
}
if len(args.ExposedEndpoints) > 0 {
app.ExposedEndpoints_ = make(map[string]*exposedEndpoint)
for key, value := range args.ExposedEndpoints {
app.ExposedEndpoints_[key] = newExposedEndpoint(value)
}
}
return app
}
// Tag implements Application.
func (a *application) Tag() names.ApplicationTag {
return names.NewApplicationTag(a.Name_)
}
// Name implements Application.
func (a *application) Name() string {
return a.Name_
}
// Type implements Application
func (a *application) Type() string {
return a.Type_
}
// Subordinate implements Application.
func (a *application) Subordinate() bool {
return a.Subordinate_
}
// CharmURL implements Application.
func (a *application) CharmURL() string {
return a.CharmURL_
}
// Channel implements Application.
func (a *application) Channel() string {
return a.Channel_
}
// CharmModifiedVersion implements Application.
func (a *application) CharmModifiedVersion() int {
return a.CharmModifiedVersion_
}
// ForceCharm implements Application.
func (a *application) ForceCharm() bool {
return a.ForceCharm_
}
// Exposed implements Application.
func (a *application) Exposed() bool {
return a.Exposed_
}
// ExposedEndpoints implements Application.
func (a *application) ExposedEndpoints() map[string]ExposedEndpoint {
if len(a.ExposedEndpoints_) == 0 {
return nil
}
result := make(map[string]ExposedEndpoint)
for key, value := range a.ExposedEndpoints_ {
result[key] = value
}
return result
}
// PasswordHash implements Application.
func (a *application) PasswordHash() string {
return a.PasswordHash_
}
// PodSpec implements Application.
func (a *application) PodSpec() string {
return a.PodSpec_
}
// Placement implements Application.
func (a *application) Placement() string {
return a.Placement_
}
// HasResources implements Application.
func (a *application) HasResources() bool {
return a.HasResources_
}
// DesiredScale implements Application.
func (a *application) DesiredScale() int {
return a.DesiredScale_
}
// MinUnits implements Application.
func (a *application) MinUnits() int {
return a.MinUnits_
}
// EndpointBindings implements Application.
func (a *application) EndpointBindings() map[string]string {
return a.EndpointBindings_
}
// ApplicationConfig implements Application.
func (a *application) ApplicationConfig() map[string]interface{} {
return a.ApplicationConfig_
}
// CharmConfig implements Application.
func (a *application) CharmConfig() map[string]interface{} {
return a.CharmConfig_
}
// Leader implements Application.
func (a *application) Leader() string {
return a.Leader_
}
// LeadershipSettings implements Application.
func (a *application) LeadershipSettings() map[string]interface{} {
return a.LeadershipSettings_
}
// StorageDirectives implements Application.
func (a *application) StorageDirectives() map[string]StorageDirective {
result := make(map[string]StorageDirective)
for key, value := range a.StorageDirectives_ {
result[key] = value
}
return result
}
// MetricsCredentials implements Application.
func (a *application) MetricsCredentials() []byte {
// Here we are explicitly throwing away any decode error. We check that
// the creds can be decoded when we parse the incoming data, or we encode
// an incoming byte array, so in both cases, we know that the stored creds
// can be decoded.
creds, _ := base64.StdEncoding.DecodeString(a.MetricsCredentials_)
return creds
}
// OpenedPortRanges implements Application.
func (a *application) OpenedPortRanges() PortRanges {
if a.OpenedPortRanges_ == nil {
a.OpenedPortRanges_ = newDeployedPortRanges()
}
return a.OpenedPortRanges_
}
// AddOpenedPortRange implements Application.
func (a *application) AddOpenedPortRange(args OpenedPortRangeArgs) {
if a.OpenedPortRanges_ == nil {
a.OpenedPortRanges_ = newDeployedPortRanges()
}
if a.OpenedPortRanges_.ByUnit_[args.UnitName] == nil {
a.OpenedPortRanges_.ByUnit_[args.UnitName] = newUnitPortRanges()
}
a.OpenedPortRanges_.ByUnit_[args.UnitName].ByEndpoint_[args.EndpointName] = append(
a.OpenedPortRanges_.ByUnit_[args.UnitName].ByEndpoint_[args.EndpointName],
newUnitPortRange(args.FromPort, args.ToPort, args.Protocol),
)
}
// OperatorStatus implements Application.
func (a *application) OperatorStatus() Status {
// To avoid typed nils check nil here.
if a.OperatorStatus_ == nil {
return nil
}
return a.OperatorStatus_
}
// SetOperatorStatus implements Application.
func (a *application) SetOperatorStatus(args StatusArgs) {
a.OperatorStatus_ = newStatus(args)
}
// Status implements Application.
func (a *application) Status() Status {
// To avoid typed nils check nil here.
if a.Status_ == nil {
return nil
}
return a.Status_
}
// SetStatus implements Application.
func (a *application) SetStatus(args StatusArgs) {
a.Status_ = newStatus(args)
}
// Units implements Application.
func (a *application) Units() []Unit {
result := make([]Unit, len(a.Units_.Units_))
for i, u := range a.Units_.Units_ {
result[i] = u
}
return result
}
func (a *application) unitNames() set.Strings {
result := set.NewStrings()
for _, u := range a.Units_.Units_ {
result.Add(u.Name())
}
return result
}
// AddUnit implements Application.
func (a *application) AddUnit(args UnitArgs) Unit {
u := newUnit(args)
a.Units_.Units_ = append(a.Units_.Units_, u)
return u
}
func (a *application) setUnits(unitList []*unit) {
a.Units_ = units{
Version: 3,
Units_: unitList,
}
}
// Constraints implements HasConstraints.
func (a *application) Constraints() Constraints {
if a.Constraints_ == nil {
return nil
}
return a.Constraints_
}
// SetConstraints implements HasConstraints.
func (a *application) SetConstraints(args ConstraintsArgs) {
a.Constraints_ = newConstraints(args)
}
// CloudService implements Application.
func (a *application) CloudService() CloudService {
if a.CloudService_ == nil {
return nil
}
return a.CloudService_
}
// SetCloudService implements Application.
func (a *application) SetCloudService(args CloudServiceArgs) {
a.CloudService_ = newCloudService(&args)
}
// Resources implements Application.
func (a *application) Resources() []Resource {
rs := a.Resources_.Resources_
result := make([]Resource, len(rs))
for i, r := range rs {
result[i] = r
}
return result
}
// AddResource implements Application.
func (a *application) AddResource(args ResourceArgs) Resource {
r := newResource(args)
a.Resources_.Resources_ = append(a.Resources_.Resources_, r)
return r
}
func (a *application) setResources(resourceList []*resource) {
a.Resources_ = resources{
Version: 1,
Resources_: resourceList,
}
}
// Tools implements Application.
func (a *application) Tools() AgentTools {
// To avoid a typed nil, check before returning.
if a.Tools_ == nil {
return nil
}
return a.Tools_
}
// SetTools implements Application.
func (a *application) SetTools(args AgentToolsArgs) {
a.Tools_ = newAgentTools(args)
}
// CharmOrigin implements Application.
func (a *application) CharmOrigin() CharmOrigin {
// To avoid a typed nil, check before returning.
if a.CharmOrigin_ == nil {
return nil
}
return a.CharmOrigin_
}
// SetCharmOrigin implements Application.
func (a *application) SetCharmOrigin(args CharmOriginArgs) {
a.CharmOrigin_ = newCharmOrigin(args)
}
// CharmMetadata implements Application.
func (a *application) CharmMetadata() CharmMetadata {
// To avoid a typed nil, check before returning.
if a.CharmMetadata_ == nil {
return nil
}
return a.CharmMetadata_
}
// SetCharmMetadata implements Application.
func (a *application) SetCharmMetadata(args CharmMetadataArgs) {
a.CharmMetadata_ = newCharmMetadata(args)
}
// CharmManifest implements Application.
func (a *application) CharmManifest() CharmManifest {
// To avoid a typed nil, check before returning.
if a.CharmManifest_ == nil {
return nil
}
return a.CharmManifest_
}
// SetCharmManifest implements Application.
func (a *application) SetCharmManifest(args CharmManifestArgs) {
a.CharmManifest_ = newCharmManifest(args)
}
// CharmActions implements Application.
func (a *application) CharmActions() CharmActions {
// To avoid a typed nil, check before returning.
if a.CharmActions_ == nil {
return nil
}
return a.CharmActions_
}
// SetCharmActions implements Application.
func (a *application) SetCharmActions(args CharmActionsArgs) {
a.CharmActions_ = newCharmActions(args)
}
// CharmConfigs implements Application.
func (a *application) CharmConfigs() CharmConfigs {
// To avoid a typed nil, check before returning.
if a.CharmConfigs_ == nil {
return nil
}
return a.CharmConfigs_
}
// SetCharmConfigs implements Application.
func (a *application) SetCharmConfigs(args CharmConfigsArgs) {
a.CharmConfigs_ = newCharmConfigs(args)
}
// Offers implements Application.
func (a *application) Offers() []ApplicationOffer {
if a.Offers_ == nil || len(a.Offers_.Offers) == 0 {
return nil
}
res := make([]ApplicationOffer, len(a.Offers_.Offers))
for i, offer := range a.Offers_.Offers {
res[i] = offer
}
return res
}
// AddOffer implements Application.
func (a *application) AddOffer(args ApplicationOfferArgs) ApplicationOffer {
if a.Offers_ == nil {
a.Offers_ = &applicationOffers{
Version: 2,
}
}
offer := newApplicationOffer(args)
a.Offers_.Offers = append(a.Offers_.Offers, offer)
return offer
}
func (a *application) setOffers(offers []*applicationOffer) {
a.Offers_ = &applicationOffers{
Version: 2,
Offers: offers,
}
}
// Validate implements Application.
func (a *application) Validate() error {
if a.Name_ == "" {
return errors.NotValidf("application missing name")
}
if a.Status_ == nil {
return errors.NotValidf("application %q missing status", a.Name_)
}
for _, resource := range a.Resources_.Resources_ {
if err := resource.Validate(); err != nil {
return errors.Annotatef(err, "resource %s", resource.Name_)
}
}
// If leader is set, it must match one of the units.
var leaderFound bool
// All of the applications units should also be valid.
for _, u := range a.Units() {
if err := u.Validate(); err != nil {
return errors.Trace(err)
}
// We know that the unit has a name, because it validated correctly.
if u.Name() == a.Leader_ {
leaderFound = true
}
}
if a.Leader_ != "" && !leaderFound {
return errors.NotValidf("missing unit for leader %q", a.Leader_)
}
return nil
}
// ProvisioningState implements Application.
func (a *application) ProvisioningState() ProvisioningState {
if a.ProvisioningState_ == nil {
return nil
}
return a.ProvisioningState_
}
func importApplications(source map[string]interface{}) ([]*application, error) {
checker := versionedChecker("applications")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "applications version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := applicationDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["applications"].([]interface{})
return importApplicationList(sourceList, importFunc)
}
func importApplicationList(sourceList []interface{}, importFunc applicationDeserializationFunc) ([]*application, error) {
result := make([]*application, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for application %d, %T", i, value)
}
application, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "application %d", i)
}
result = append(result, application)
}
return result, nil
}
type applicationDeserializationFunc func(map[string]interface{}) (*application, error)
var applicationDeserializationFuncs = map[int]applicationDeserializationFunc{
1: importApplicationV1,
2: importApplicationV2,
3: importApplicationV3,
4: importApplicationV4,
5: importApplicationV5,
6: importApplicationV6,
7: importApplicationV7,
8: importApplicationV8,
9: importApplicationV9,
10: importApplicationV10,
11: importApplicationV11,
12: importApplicationV12,
13: importApplicationV13,
}
func applicationV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"name": schema.String(),
"series": schema.String(),
"subordinate": schema.Bool(),
"charm-url": schema.String(),
"cs-channel": schema.String(),
"charm-mod-version": schema.Int(),
"force-charm": schema.Bool(),
"exposed": schema.Bool(),
"min-units": schema.Int(),
"status": schema.StringMap(schema.Any()),
"endpoint-bindings": schema.StringMap(schema.String()),
"settings": schema.StringMap(schema.Any()),
"leader": schema.String(),
"leadership-settings": schema.StringMap(schema.Any()),
"storage-constraints": schema.StringMap(schema.StringMap(schema.Any())),
"metrics-creds": schema.String(),
"resources": schema.StringMap(schema.Any()),
"units": schema.StringMap(schema.Any()),
}
defaults := schema.Defaults{
"subordinate": false,
"force-charm": false,
"exposed": false,
"min-units": int64(0),
"leader": "",
"metrics-creds": "",
"storage-constraints": schema.Omit,
"endpoint-bindings": schema.Omit,
"application-config": schema.Omit,
}
addAnnotationSchema(fields, defaults)
addConstraintsSchema(fields, defaults)
addStatusHistorySchema(fields)
return fields, defaults
}
func applicationV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV1Fields()
fields["type"] = schema.String()
return fields, defaults
}
func applicationV3Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV2Fields()
fields["application-config"] = schema.StringMap(schema.Any())
fields["password-hash"] = schema.String()
fields["pod-spec"] = schema.String()
fields["cloud-service"] = schema.StringMap(schema.Any())
fields["tools"] = schema.StringMap(schema.Any())
defaults["password-hash"] = ""
defaults["pod-spec"] = ""
defaults["cloud-service"] = schema.Omit
defaults["tools"] = schema.Omit
return fields, defaults
}
func applicationV4Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV3Fields()
fields["placement"] = schema.String()
fields["desired-scale"] = schema.Int()
fields["operator-status"] = schema.StringMap(schema.Any())
defaults["placement"] = ""
defaults["desired-scale"] = int64(0)
defaults["operator-status"] = schema.Omit
return fields, defaults
}
func applicationV5Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV4Fields()
fields["offers"] = schema.StringMap(schema.Any())
defaults["offers"] = schema.Omit
return fields, defaults
}
func applicationV6Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV5Fields()
fields["has-resources"] = schema.Bool()
defaults["has-resources"] = false
return fields, defaults
}
func applicationV7Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV6Fields()
fields["charm-origin"] = schema.StringMap(schema.Any())
defaults["charm-origin"] = schema.Omit
return fields, defaults
}
func applicationV8Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV7Fields()
fields["exposed-endpoints"] = schema.StringMap(schema.StringMap(schema.Any()))
defaults["exposed-endpoints"] = schema.Omit
return fields, defaults
}
func applicationV9Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV8Fields()
delete(fields, "series")
defaults["series"] = schema.Omit
return fields, defaults
}
func applicationV10Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV9Fields()
fields["opened-port-ranges"] = schema.StringMap(schema.Any())
defaults["opened-port-ranges"] = schema.Omit
return fields, defaults
}
func applicationV11Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV10Fields()
fields["provisioning-state"] = schema.StringMap(schema.Any())
defaults["provisioning-state"] = schema.Omit
return fields, defaults
}
func applicationV12Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV11Fields()
delete(fields, "storage-constraints")
defaults["storage-constraints"] = schema.Omit
fields["storage-directives"] = schema.StringMap(schema.StringMap(schema.Any()))
defaults["storage-directives"] = schema.Omit
return fields, defaults
}
func applicationV13Fields() (schema.Fields, schema.Defaults) {
fields, defaults := applicationV12Fields()
fields["charm-metadata"] = schema.StringMap(schema.Any())
fields["charm-manifest"] = schema.StringMap(schema.Any())
fields["charm-actions"] = schema.StringMap(schema.Any())
fields["charm-configs"] = schema.StringMap(schema.Any())
defaults["charm-metadata"] = schema.Omit
defaults["charm-manifest"] = schema.Omit
defaults["charm-actions"] = schema.Omit
defaults["charm-configs"] = schema.Omit
return fields, defaults
}
func importApplicationV1(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV1Fields()
return importApplication(fields, defaults, 1, source)
}
func importApplicationV2(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV2Fields()
return importApplication(fields, defaults, 2, source)
}
func importApplicationV3(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV3Fields()
return importApplication(fields, defaults, 3, source)
}
func importApplicationV4(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV4Fields()
return importApplication(fields, defaults, 4, source)
}
func importApplicationV5(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV5Fields()
return importApplication(fields, defaults, 5, source)
}
func importApplicationV6(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV6Fields()
return importApplication(fields, defaults, 6, source)
}
func importApplicationV7(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV7Fields()
return importApplication(fields, defaults, 7, source)
}
func importApplicationV8(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV8Fields()
return importApplication(fields, defaults, 8, source)
}
func importApplicationV9(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV9Fields()
return importApplication(fields, defaults, 9, source)
}
func importApplicationV10(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV10Fields()
return importApplication(fields, defaults, 10, source)
}
func importApplicationV11(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV11Fields()
return importApplication(fields, defaults, 11, source)
}
func importApplicationV12(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV12Fields()
return importApplication(fields, defaults, 12, source)
}
func importApplicationV13(source map[string]interface{}) (*application, error) {
fields, defaults := applicationV13Fields()
return importApplication(fields, defaults, 13, source)
}
func importApplication(fields schema.Fields, defaults schema.Defaults, importVersion int, source map[string]interface{}) (*application, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "application schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &application{
Name_: valid["name"].(string),
Type_: IAAS,
Subordinate_: valid["subordinate"].(bool),
CharmURL_: valid["charm-url"].(string),
Channel_: valid["cs-channel"].(string),
CharmModifiedVersion_: int(valid["charm-mod-version"].(int64)),
ForceCharm_: valid["force-charm"].(bool),
Exposed_: valid["exposed"].(bool),
MinUnits_: int(valid["min-units"].(int64)),
EndpointBindings_: convertToStringMap(valid["endpoint-bindings"]),
CharmConfig_: valid["settings"].(map[string]interface{}),
Leader_: valid["leader"].(string),
LeadershipSettings_: valid["leadership-settings"].(map[string]interface{}),
StatusHistory_: newStatusHistory(),
}
if importVersion >= 2 {
result.Type_ = valid["type"].(string)
}
if importVersion >= 3 {
result.PasswordHash_ = valid["password-hash"].(string)
result.PodSpec_ = valid["pod-spec"].(string)
}
if importVersion >= 4 {
result.Placement_ = valid["placement"].(string)
result.DesiredScale_ = int(valid["desired-scale"].(int64))
if operatorStatus, ok := valid["operator-status"].(map[string]interface{}); ok {
status, err := importStatus(operatorStatus)
if err != nil {
return nil, errors.Trace(err)
}
result.OperatorStatus_ = status
}
}
if importVersion >= 5 {
if offerMap, ok := valid["offers"]; ok {
offers, err := importApplicationOffers(offerMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.setOffers(offers)
}
}
if importVersion >= 6 {
result.HasResources_ = valid["has-resources"].(bool)
}
if importVersion >= 7 {
if charmOriginMap, ok := valid["charm-origin"]; ok {
charmOrigin, err := importCharmOrigin(charmOriginMap.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.CharmOrigin_ = charmOrigin
}
}
if importVersion >= 8 {
if exposedEndpoints, ok := valid["exposed-endpoints"].(map[string]interface{}); ok {