@@ -91,8 +91,8 @@ def fail_wanted_one(self, response, endpoint, query_params):
91
91
total_results = response .json ["count" ],
92
92
)
93
93
94
- def get_one (
95
- self , endpoint , name = None , allow_none = True , check_exists = False , ** kwargs
94
+ def get_one_or_many (
95
+ self , endpoint , name = None , allow_none = True , check_exists = False , want_one = True , ** kwargs
96
96
):
97
97
new_kwargs = kwargs .copy ()
98
98
response = None
@@ -103,44 +103,48 @@ def get_one(
103
103
new_data ["{0}" .format (name_field )] = name
104
104
new_kwargs ["data" ] = new_data
105
105
106
- response = self .get_endpoint (endpoint , ** new_kwargs )
106
+ response = self .get_endpoint (endpoint , ** new_kwargs )
107
107
108
- if response .status != 200 :
109
- fail_msg = "Got a {0} when trying to get from {1}" .format (
110
- response .status , endpoint
111
- )
112
- if "detail" in response .json :
113
- fail_msg += ",detail: {0}" .format (response .json ["detail" ])
114
- self .module .fail_json (msg = fail_msg )
108
+ if response .status != 200 :
109
+ fail_msg = "Got a {0} when trying to get from {1}" .format (
110
+ response .status , endpoint
111
+ )
112
+ if "detail" in response .json :
113
+ fail_msg += ",detail: {0}" .format (response .json ["detail" ])
114
+ self .module .fail_json (msg = fail_msg )
115
115
116
- if "count" not in response .json or "results" not in response .json :
117
- self .module .fail_json (msg = "The endpoint did not provide count, results" )
116
+ logging .debug (response .json )
117
+
118
+ if "count" not in response .json or "results" not in response .json :
119
+ self .module .fail_json (msg = "The endpoint did not provide count, results" )
118
120
119
121
if response .json ["count" ] == 0 :
120
122
if allow_none :
121
123
return None
122
124
else :
123
125
self .fail_wanted_one (response , endpoint , new_kwargs .get ("data" ))
126
+ if response .json ["count" ] == 1 :
127
+ return response .json ["results" ][0 ]
124
128
elif response .json ["count" ] > 1 :
125
- if name :
126
- # Since we did a name or ID search and got > 1 return
127
- # something if the id matches
128
- for asset in response .json ["results" ]:
129
- if str (asset ["id" ]) == name :
130
- return asset
131
- # We got > 1 and either didn't find something by ID (which means
132
- # multiple names)
133
- # Or we weren't running with a or search and just got back too
134
- # many to begin with.
135
- self .fail_wanted_one (response , endpoint , new_kwargs .get ("data" ))
129
+ if want_one :
130
+ if name :
131
+ # Since we did a name or ID search and got > 1 return
132
+ # something if the id matches
133
+ for asset in response .json ["results" ]:
134
+ if str (asset ["id" ]) == name :
135
+ return asset
136
+ # We got > 1 and either didn't find something by ID (which means
137
+ # multiple names)
138
+ # Or we weren't running with a or search and just got back too
139
+ # many to begin with.
140
+ self .fail_wanted_one (response , endpoint , new_kwargs .get ("data" ))
141
+ else :
142
+ return response .json ["results" ]
136
143
137
144
if check_exists :
138
145
self .json_output ["id" ] = response .json ["results" ][0 ]["id" ]
139
146
self .module .exit_json (** self .json_output )
140
147
141
- return response .json ["results" ][0 ]
142
-
143
-
144
148
def create_if_needed (
145
149
self ,
146
150
existing_item ,
0 commit comments