1
1
import { assign } from '@ember/polyfills' ;
2
2
import { assert } from '@ember/debug' ;
3
+ import ControlGroupError from 'vault/lib/control-group-error' ;
3
4
import ApplicationAdapter from '../application' ;
4
5
import { allSettled } from 'rsvp' ;
5
6
import { addToArray } from 'vault/helpers/add-to-array' ;
@@ -24,11 +25,31 @@ export default ApplicationAdapter.extend({
24
25
} ,
25
26
26
27
staticRoles ( backend , id ) {
27
- return this . ajax ( this . urlFor ( backend , id , 'static' ) , 'GET' , this . optionsForQuery ( id ) ) ;
28
+ return this . ajax ( this . urlFor ( backend , id , 'static' ) , 'GET' , this . optionsForQuery ( id ) ) . then ( resp => {
29
+ if ( id ) {
30
+ return {
31
+ ...resp ,
32
+ type : 'static' ,
33
+ backend,
34
+ id,
35
+ } ;
36
+ }
37
+ return resp ;
38
+ } ) ;
28
39
} ,
29
40
30
41
dynamicRoles ( backend , id ) {
31
- return this . ajax ( this . urlFor ( backend , id ) , 'GET' , this . optionsForQuery ( id ) ) ;
42
+ return this . ajax ( this . urlFor ( backend , id ) , 'GET' , this . optionsForQuery ( id ) ) . then ( resp => {
43
+ if ( id ) {
44
+ return {
45
+ ...resp ,
46
+ type : 'dynamic' ,
47
+ backend,
48
+ id,
49
+ } ;
50
+ }
51
+ return resp ;
52
+ } ) ;
32
53
} ,
33
54
34
55
optionsForQuery ( id ) {
@@ -39,39 +60,43 @@ export default ApplicationAdapter.extend({
39
60
return { data } ;
40
61
} ,
41
62
42
- fetchByQuery ( store , query ) {
43
- const { backend, id } = query ;
44
- return this . ajax ( this . urlFor ( backend , id ) , 'GET' , this . optionsForQuery ( id ) ) . then ( resp => {
45
- resp . id = id ;
46
- resp . backend = backend ;
47
- return resp ;
48
- } ) ;
49
- } ,
50
-
51
63
queryRecord ( store , type , query ) {
52
64
const { backend, id } = query ;
53
- const staticReq = this . staticRoles ( backend , id ) ;
54
- const dynamicReq = this . dynamicRoles ( backend , id ) ;
55
65
56
- return allSettled ( [ staticReq , dynamicReq ] ) . then ( ( [ staticResp , dynamicResp ] ) => {
57
- if ( ! staticResp . value && ! dynamicResp . value ) {
58
- // Throw error, both reqs failed
59
- throw dynamicResp . reason ;
66
+ if ( query . type === 'static' ) {
67
+ return this . staticRoles ( backend , id ) ;
68
+ } else if ( query ?. type === 'dynamic' ) {
69
+ return this . dynamicRoles ( backend , id ) ;
70
+ }
71
+ // if role type is not defined, try both
72
+ return allSettled ( [ this . staticRoles ( backend , id ) , this . dynamicRoles ( backend , id ) ] ) . then (
73
+ ( [ staticResp , dynamicResp ] ) => {
74
+ if ( staticResp . state === 'rejected' && dynamicResp . state === 'rejected' ) {
75
+ let reason = staticResp . reason ;
76
+ if ( dynamicResp . reason instanceof ControlGroupError ) {
77
+ throw dynamicResp . reason ;
78
+ }
79
+ if ( reason ?. httpStatus < dynamicResp . reason ?. httpStatus ) {
80
+ reason = dynamicResp . reason ;
81
+ }
82
+ throw reason ;
83
+ }
84
+ // Names are distinct across both types of role,
85
+ // so only one request should ever come back with value
86
+ let type = staticResp . value ? 'static' : 'dynamic' ;
87
+ let successful = staticResp . value || dynamicResp . value ;
88
+ let resp = {
89
+ data : { } ,
90
+ backend,
91
+ id,
92
+ type,
93
+ } ;
94
+
95
+ resp . data = assign ( { } , successful . data ) ;
96
+
97
+ return resp ;
60
98
}
61
- // Names are distinct across both types of role,
62
- // so only one request should ever come back with value
63
- let type = staticResp . value ? 'static' : 'dynamic' ;
64
- let successful = staticResp . value || dynamicResp . value ;
65
- let resp = {
66
- data : { } ,
67
- backend,
68
- secret : id ,
69
- } ;
70
-
71
- resp . data = assign ( { } , resp . data , successful . data , { backend, type, secret : id } ) ;
72
-
73
- return resp ;
74
- } ) ;
99
+ ) ;
75
100
} ,
76
101
77
102
query ( store , type , query ) {
0 commit comments