summaryrefslogtreecommitdiffstats
path: root/compass-tasks-base/apiclient/restful.py
blob: bb82922c2e1b377116e23a9c679cd019ac33df0f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Compass api client library.
"""

import json
import logging
import requests


class Client(object):
    """compass restful api wrapper"""

    def __init__(self, url, headers=None, proxies=None, stream=None):
        logging.info('create api client %s', url)
        self.url_ = url
        self.session_ = requests.Session()

        if headers:
            self.session_.headers.update(headers)
        self.session_.headers.update({
            'Accept': 'application/json'
        })

        if proxies is not None:
            self.session_.proxies = proxies

        if stream is not None:
            self.session_.stream = stream

    def __del__(self):
        self.session_.close()

    @classmethod
    def _get_response(cls, resp):
        response_object = {}
        try:
            response_object = resp.json()
        except Exception as error:
            logging.error('failed to load object from %s: %s',
                          resp.url, resp.content)
            logging.exception(error)
            response_object['status'] = 'Json Parsing Failed'
            response_object['message'] = resp.content

        return resp.status_code, response_object

    def _get(self, req_url, data=None):
        url = '%s%s' % (self.url_, req_url)
        logging.debug('get %s with data %s', url, data)
        if data:
            resp = self.session_.get(url, params=data)
        else:
            resp = self.session_.get(url)

        return self._get_response(resp)

    def _post(self, req_url, data=None):
        url = '%s%s' % (self.url_, req_url)
        logging.debug('post %s with data %s', url, data)
        if data:
            resp = self.session_.post(url, json.dumps(data))
        else:
            resp = self.session_.post(url)

        return self._get_response(resp)

    def _put(self, req_url, data=None):
        """encapsulate put method."""
        url = '%s%s' % (self.url_, req_url)
        logging.debug('put %s with data %s', url, data)
        if data:
            resp = self.session_.put(url, json.dumps(data))
        else:
            resp = self.session_.put(url)

        return self._get_response(resp)

    def _patch(self, req_url, data=None):
        url = '%s%s' % (self.url_, req_url)
        logging.debug('patch %s with data %s', url, data)
        if data:
            resp = self.session_.patch(url, json.dumps(data))
        else:
            resp = self.session_.patch(url)

        return self._get_response(resp)

    def _delete(self, req_url):
        url = '%s%s' % (self.url_, req_url)
        logging.debug('delete %s', url)
        return self._get_response(self.session_.delete(url))

    def login(self, email, password):
        credential = {}
        credential['email'] = email
        credential['password'] = password
        return self._post('/users/login', data=credential)

    def get_token(self, email, password):
        credential = {}
        credential['email'] = email
        credential['password'] = password
        status, resp = self._post('/users/token', data=credential)
        if status < 400:
            self.session_.headers.update({'X-Auth-Token': resp['token']})
        return status, resp

    def get_users(self):
        users = self._get('/users')
        return users

    def list_switches(
            self,
            switch_ips=None,
            switch_ip_networks=None):
        """list switches."""
        params = {}
        if switch_ips:
            params['switchIp'] = switch_ips

        if switch_ip_networks:
            params['switchIpNetwork'] = switch_ip_networks

        switchlist = self._get('/switches', data=params)
        return switchlist

    def get_switch(self, switch_id):
        return self._get('/switches/%s' % switch_id)

    def add_switch(
            self,
            switch_ip,
            version=None,
            community=None,
            raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            data['ip'] = switch_ip
            data['credentials'] = {}
            if version:
                data['credentials']['version'] = version

            if community:
                data['credentials']['community'] = community

        return self._post('/switches', data=data)

    def update_switch(self, switch_id, state='initialized',
                      version='2c', community='public', raw_data={}):
        data = {}
        if raw_data:
            data = raw_data

        else:
            data['credentials'] = {}
            if version:
                data['credentials']['version'] = version

            if community:
                data['credentials']['community'] = community

            if state:
                data['state'] = state

        return self._put('/switches/%s' % switch_id, data=data)

    def delete_switch(self, switch_id):
        return self._delete('/switches/%s' % switch_id)

    def list_switch_machines(self, switch_id, port=None, vlans=None,
                             tag=None, location=None):
        data = {}
        if port:
            data['port'] = port

        if vlans:
            data['vlans'] = vlans

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        return self._get('/switches/%s/machines' % switch_id, data=data)

    def get_switch_machine(self, switch_id, machine_id):
        return self._get('/switches/%s/machines/%s' % (switch_id, machine_id))

    def list_switch_machines_hosts(self, switch_id, port=None, vlans=None,
                                   mac=None, tag=None, location=None,
                                   os_name=None, os_id=None):

        data = {}
        if port:
            data['port'] = port

        if vlans:
            data['vlans'] = vlans

        if mac:
            data['mac'] = mac

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        if os_name:
            data['os_name'] = os_name

        if os_id:
            data['os_id'] = os_id

        return self._get('/switches/%s/machines-hosts' % switch_id, data=data)

    def add_switch_machine(self, switch_id, mac=None, port=None,
                           vlans=None, ipmi_credentials=None,
                           tag=None, location=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if mac:
                data['mac'] = mac

            if port:
                data['port'] = port

            if vlans:
                data['vlans'] = vlans

            if ipmi_credentials:
                data['ipmi_credentials'] = ipmi_credentials

            if tag:
                data['tag'] = tag

            if location:
                data['location'] = location

        return self._post('/switches/%s/machines' % switch_id, data=data)

    def update_switch_machine(self, switch_id, machine_id, port=None,
                              vlans=None, ipmi_credentials=None, tag=None,
                              location=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if port:
                data['port'] = port

            if vlans:
                data['vlans'] = vlans

            if ipmi_credentials:
                data['ipmi_credentials'] = ipmi_credentials

            if tag:
                data['tag'] = tag

            if location:
                data['location'] = location

        return self._put('/switches/%s/machines/%s' %
                         (switch_id, machine_id), data=data)

    def delete_switch_machine(self, switch_id, machine_id):
        return self._delete('/switches/%s/machines/%s' %
                            (switch_id, machine_id))

    # test these
    def poll_switch(self, switch_id):
        data = {}
        data['find_machines'] = None
        return self._post('/switches/%s/action' % switch_id, data=data)

    def add_group_switch_machines(self, switch_id, group_machine_ids):
        data = {}
        data['add_machines'] = group_machine_ids
        return self._post('/switches/%s/action' % switch_id, data=data)

    def remove_group_switch_machines(self, switch_id, group_machine_ids):
        data = {}
        data['remove_machines'] = group_machine_ids
        return self._post('/switches/%s/action' % switch_id, data=data)

    def update_group_switch_machines(self, switch_id, group_machines):
        data = {}
        data['set_machines'] = group_machines
        return self._post('/switches/%s/action' % switch_id, data=data)
    # end

    def list_switchmachines(self, switch_ip_int=None, port=None, vlans=None,
                            mac=None, tag=None, location=None):
        data = {}
        if switch_ip_int:
            data['switch_ip_int'] = switch_ip_int

        if port:
            data['port'] = port

        if vlans:
            data['vlans'] = vlans

        if mac:
            data['mac'] = mac

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        return self._get('/switch-machines', data=data)

    def list_switchmachines_hosts(self, switch_ip_int=None, port=None,
                                  vlans=None, mac=None, tag=None,
                                  location=None, os_name=None, os_id=None):

        data = {}
        if switch_ip_int:
            data['switch_ip_int'] = switch_ip_int

        if port:
            data['port'] = port

        if vlans:
            data['vlans'] = vlans

        if mac:
            data['mac'] = mac

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        if os_name:
            data['os_name'] = os_name

        if os_id:
            data['os_id'] = os_id

        return self._get('/switches-machines-hosts', data=data)

    def show_switchmachine(self, switchmachine_id):
        return self._get('/switch-machines/%s' % switchmachine_id)

    def update_switchmachine(self, switchmachine_id,
                             port=None, vlans=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if port:
                data['port'] = port

            if vlans:
                data['vlans'] = vlans

        return self._put('/switch-machines/%s' % switchmachine_id, data=data)

    def patch_switchmachine(self, switchmachine_id,
                            vlans=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        elif vlans:
            data['vlans'] = vlans

        return self._patch('/switch-machines/%s' % switchmachine_id, data=data)

    def delete_switchmachine(self, switchmachine_id):
        return self._delete('/switch-machines/%s' % switchmachine_id)

    def list_machines(self, mac=None, tag=None, location=None):
        data = {}
        if mac:
            data['mac'] = mac

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        return self._get('/machines', data=data)

    def get_machine(self, machine_id):
        data = {}
        if id:
            data['id'] = id

        return self._get('/machines/%s' % machine_id, data=data)

    def update_machine(self, machine_id, ipmi_credentials=None, tag=None,
                       location=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if ipmi_credentials:
                data['ipmi_credentials'] = ipmi_credentials

            if tag:
                data['tag'] = tag

            if location:
                data['location'] = location

        return self._put('/machines/%s' % machine_id, data=data)

    def patch_machine(self, machine_id, ipmi_credentials=None,
                      tag=None, location=None,
                      raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if ipmi_credentials:
                data['ipmi_credentials'] = ipmi_credentials

            if tag:
                data['tag'] = tag

            if location:
                data['location'] = location

        return self._patch('/machines/%s' % machine_id, data=data)

    def delete_machine(self, machine_id):
        return self._delete('machines/%s' % machine_id)

    def list_subnets(self, subnet=None, name=None):
        data = {}
        if subnet:
            data['subnet'] = subnet

        if name:
            data['name'] = name

        return self._get('/subnets', data=data)

    def get_subnet(self, subnet_id):
        return self._get('/subnets/%s' % subnet_id)

    def add_subnet(self, subnet, name=None, raw_data=None):
        data = {}
        data['subnet'] = subnet
        if raw_data:
            data.update(raw_data)
        else:
            if name:
                data['name'] = name

        return self._post('/subnets', data=data)

    def update_subnet(self, subnet_id, subnet=None,
                      name=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if subnet:
                data['subnet'] = subnet

            if name:
                data['name'] = name
        return self._put('/subnets/%s' % subnet_id, data=data)

    def delete_subnet(self, subnet_id):
        return self._delete('/subnets/%s' % subnet_id)

    def list_adapters(self, name=None):
        data = {}
        if name:
            data['name'] = name

        return self._get('/adapters', data=data)

    def get_adapter(self, adapter_id):
        return self._get('/adapters/%s' % adapter_id)

    def get_adapter_roles(self, adapter_id):
        return self._get('/adapters/%s/roles' % adapter_id)

    def get_adapter_metadata(self, adapter_id):
        return self._get('/adapters/%s/metadata' % adapter_id)

    def get_os_metadata(self, os_id):
        return self._get('/oses/%s/metadata' % os_id)

    def list_clusters(self, name=None, os_name=None,
                      owner=None,
                      adapter_id=None):
        data = {}
        if name:
            data['name'] = name

        if os_name:
            data['os_name'] = os_name

        if owner:
            data['owner'] = owner

        if adapter_id:
            data['adapter_id'] = adapter_id

        return self._get('/clusters', data=data)

    def get_cluster(self, cluster_id):
        return self._get('/clusters/%s' % cluster_id)

    def add_cluster(self, name, adapter_id, os_id,
                    flavor_id=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if flavor_id:
                data['flavor_id'] = flavor_id
            data['name'] = name
            data['adapter_id'] = adapter_id
            data['os_id'] = os_id

        return self._post('/clusters', data=data)

    def update_cluster(self, cluster_id, name=None,
                       reinstall_distributed_system=None,
                       raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if name:
                data['name'] = name

            if reinstall_distributed_system:
                data['reinstall_distributed_system'] = (
                    reinstall_distributed_system
                )
        return self._put('/clusters/%s' % cluster_id, data=data)

    def delete_cluster(self, cluster_id):
        return self._delete('/clusters/%s' % cluster_id)

    def get_cluster_config(self, cluster_id):
        return self._get('/clusters/%s/config' % cluster_id)

    def get_cluster_metadata(self, cluster_id):
        return self._get('/clusters/%s/metadata' % cluster_id)

    def update_cluster_config(self, cluster_id, os_config=None,
                              package_config=None, config_step=None,
                              raw_data=None):
        data = {}
        if raw_data:
            data = raw_data

        if os_config:
            data['os_config'] = os_config

        if package_config:
            data['package_config'] = package_config

        if config_step:
            data['config_step'] = config_step

        return self._put('/clusters/%s/config' % cluster_id, data=data)

    def patch_cluster_config(self, cluster_id, os_config=None,
                             package_config=None, config_step=None,
                             raw_data=None):
        data = {}
        if raw_data:
            data = raw_data

        if os_config:
            data['os_config'] = os_config

        if package_config:
            data['package_config'] = package_config

        if config_step:
            data['config_step'] = config_step

        return self._patch('/clusters/%s/config' % cluster_id, data=data)

    def delete_cluster_config(self, cluster_id):
        return self._delete('/clusters/%s/config' % cluster_id)

    # test these
    def add_hosts_to_cluster(self, cluster_id, hosts):
        data = {}
        data['add_hosts'] = hosts
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def set_hosts_in_cluster(self, cluster_id, hosts):
        data = {}
        data['set_hosts'] = hosts
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def remove_hosts_from_cluster(self, cluster_id, hosts):
        data = {}
        data['remove_hosts'] = hosts
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def review_cluster(self, cluster_id, review={}):
        data = {}
        data['review'] = review
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def deploy_cluster(self, cluster_id, deploy={}):
        data = {}
        data['deploy'] = deploy
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def redeploy_cluster(self, cluster_id, deploy={}):
        data = {}
        data['redeploy'] = deploy
        return self._post('/clusters/%s/action' % cluster_id, data=data)

    def get_cluster_state(self, cluster_id):
        return self._get('/clusters/%s/state' % cluster_id)

    def list_cluster_hosts(self, cluster_id):
        return self._get('/clusters/%s/hosts' % cluster_id)

    def list_clusterhosts(self):
        return self._get('/clusterhosts')

    def get_cluster_host(self, cluster_id, host_id):
        return self._get('/clusters/%s/hosts/%s' % (cluster_id, host_id))

    def get_clusterhost(self, clusterhost_id):
        return self._get('/clusterhosts/%s' % clusterhost_id)

    def add_cluster_host(self, cluster_id, machine_id=None, name=None,
                         reinstall_os=None, raw_data=None):
        data = {}
        data['machine_id'] = machine_id
        if raw_data:
            data.update(raw_data)
        else:
            if name:
                data['name'] = name

            if reinstall_os:
                data['reinstall_os'] = reinstall_os

        return self._post('/clusters/%s/hosts' % cluster_id, data=data)

    def delete_cluster_host(self, cluster_id, host_id):
        return self._delete('/clusters/%s/hosts/%s' %
                            (cluster_id, host_id))

    def delete_clusterhost(self, clusterhost_id):
        return self._delete('/clusterhosts/%s' % clusterhost_id)

    def get_cluster_host_config(self, cluster_id, host_id):
        return self._get('/clusters/%s/hosts/%s/config' %
                         (cluster_id, host_id))

    def get_clusterhost_config(self, clusterhost_id):
        return self._get('/clusterhosts/%s/config' % clusterhost_id)

    def update_cluster_host_config(self, cluster_id, host_id,
                                   os_config=None,
                                   package_config=None,
                                   raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if os_config:
                data['os_config'] = os_config

            if package_config:
                data['package_config'] = package_config

        return self._put('/clusters/%s/hosts/%s/config' %
                         (cluster_id, host_id), data=data)

    def update_clusterhost_config(self, clusterhost_id, os_config=None,
                                  package_config=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data

        else:
            if os_config:
                data['os_config'] = os_config

            if package_config:
                data['package_config'] = package_config

        return self._put('/clusterhosts/%s/config' % clusterhost_id,
                         data=data)

    def patch_cluster_host_config(self, cluster_id, host_id,
                                  os_config=None,
                                  package_config=None,
                                  raw_data=None):
        data = {}
        if raw_data:
            data = raw_data

        else:
            if os_config:
                data['os_config'] = os_config

            if package_config:
                data['package_config'] = package_config

        return self._patch('/clusters/%s/hosts/%s/config' %
                           (cluster_id, host_id), data=data)

    def patch_clusterhost_config(self, clusterhost_id, os_config=None,
                                 package_config=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data

        else:
            if os_config:
                data['os_config'] = os_config

            if package_config:
                data['package_config'] = package_config

        return self._patch('/clusterhosts/%s' % clusterhost_id, data=data)

    def delete_cluster_host_config(self, cluster_id, host_id):
        return self._delete('/clusters/%s/hosts/%s/config' %
                            (cluster_id, host_id))

    def delete_clusterhost_config(self, clusterhost_id):
        return self._delete('/clusterhosts/%s/config' % clusterhost_id)

    def get_cluster_host_state(self, cluster_id, host_id):
        return self._get('/clusters/%s/hosts/%s/state' %
                         (cluster_id, host_id))

    def update_cluster_host(self, cluster_id, host_id,
                            roles=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if roles:
                data['roles'] = roles

        return self._put('/clusters/%s/hosts/%s' %
                         (cluster_id, host_id), data=data)

    def update_clusterhost(self, clusterhost_id,
                           roles=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if roles:
                data['roles'] = roles

        return self._put('/clusterhosts/%s' % clusterhost_id, data=data)

    def patch_cluster_host(self, cluster_id, host_id,
                           roles=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if roles:
                data['roles'] = roles

        return self._patch('/clusters/%s/hosts/%s' %
                           (cluster_id, host_id), data=data)

    def patch_clusterhost(self, clusterhost_id,
                          roles=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if roles:
                data['roles'] = roles

        return self._patch('/clusterhosts/%s' % clusterhost_id, data=data)

    def get_clusterhost_state(self, clusterhost_id):
        return self._get('/clusterhosts/%s/state' % clusterhost_id)

    def update_cluster_host_state(self, cluster_id, host_id, state=None,
                                  percentage=None, message=None,
                                  raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if state:
                data['state'] = state

            if percentage:
                data['percentage'] = percentage

            if message:
                data['message'] = message

        return self._put('/clusters/%s/hosts/%s/state' % (cluster_id, host_id),
                         data=data)

    def update_clusterhost_state(self, clusterhost_id, state=None,
                                 percentage=None, message=None,
                                 raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if state:
                data['state'] = state

            if percentage:
                data['percentage'] = percentage

            if message:
                data['message'] = message

        return self._put('/clusterhosts/%s/state' % clusterhost_id, data=data)

    def list_hosts(self, name=None, os_name=None, owner=None, mac=None):
        data = {}
        if name:
            data['name'] = name

        if os_name:
            data['os_name'] = os_name

        if owner:
            data['owner'] = owner

        if mac:
            data['mac'] = mac

        return self._get('/hosts', data=data)

    def get_host(self, host_id):
        return self._get('/hosts/%s' % host_id)

    def list_machines_or_hosts(self, mac=None, tag=None,
                               location=None, os_name=None,
                               os_id=None):
        data = {}
        if mac:
            data['mac'] = mac

        if tag:
            data['tag'] = tag

        if location:
            data['location'] = location

        if os_name:
            data['os_name'] = os_name

        if os_id:
            data['os_id'] = os_id

        return self._get('/machines-hosts', data=data)

    def get_machine_or_host(self, host_id):
        return self._get('/machines-hosts/%s' % host_id)

    def update_host(self, host_id, name=None,
                    reinstall_os=None, raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if name:
                data['name'] = name

            if reinstall_os:
                data['reinstall_os'] = reinstall_os

        return self._put('/hosts/%s' % host_id, data=data)

    def delete_host(self, host_id):
        return self._delete('/hosts/%s' % host_id)

    def get_host_clusters(self, host_id):
        return self._get('/hosts/%s/clusters' % host_id)

    def get_host_config(self, host_id):
        return self._get('/hosts/%s/config' % host_id)

    def update_host_config(self, host_id, os_config, raw_data=None):
        data = {}
        data['os_config'] = os_config
        if raw_data:
            data.update(raw_data)

        return self._put('/hosts/%s/config' % host_id, data=data)

    def patch_host_config(self, host_id, os_config, raw_data=None):
        data = {}
        data['os_config'] = os_config
        if raw_data:
            data.update(raw_data)

        return self._patch('/hosts/%s/config' % host_id, data=data)

    def delete_host_config(self, host_id):
        return self._delete('/hosts/%s/config' % host_id)

    def list_host_networks(self, host_id, interface=None, ip=None,
                           subnet=None, is_mgmt=None, is_promiscuous=None):
        data = {}
        if interface:
            data['interface'] = interface

        if ip:
            data['ip'] = ip

        if subnet:
            data['subnet'] = subnet

        if is_mgmt:
            data['is_mgmt'] = is_mgmt

        if is_promiscuous:
            data['is_promiscuous'] = is_promiscuous

        return self._get('/hosts/%s/networks' % host_id, data=data)

    def list_all_host_networks(self, interface=None, ip=None, subnet=None,
                               is_mgmt=None, is_promiscuous=None):
        data = {}
        if interface:
            data['interface'] = interface

        if ip:
            data['ip'] = ip

        if subnet:
            data['subnet'] = subnet

        if is_mgmt:
            data['is_mgmt'] = is_mgmt

        if is_promiscuous:
            data['is_promiscuous'] = is_promiscuous

        return self._get('/host-networks', data=data)

    def get_host_network(self, host_id, host_network_id):
        return self._get('/hosts/%s/networks/%s' %
                         (host_id, host_network_id))

    def get_network_for_all_hosts(self, host_network_id):
        return self._get('/host-networks/%s' % host_network_id)

    def add_host_network(self, host_id, interface, ip, subnet_id,
                         is_mgmt=None, is_promiscuous=None,
                         raw_data=None):
        data = {}
        data['interface'] = interface
        data['ip'] = ip
        data['subnet_id'] = subnet_id
        if raw_data:
            data.update(raw_data)
        else:
            if is_mgmt:
                data['is_mgmt'] = is_mgmt

            if is_promiscuous:
                data['is_promiscuous'] = is_promiscuous

        return self._post('/hosts/%s/networks' % host_id, data=data)

    def update_host_network(self, host_id, host_network_id,
                            ip=None, subnet_id=None, subnet=None,
                            is_mgmt=None, is_promiscuous=None,
                            raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if ip:
                data['ip'] = ip

            if subnet_id:
                data['subnet_id'] = subnet_id

            if subnet:
                data['subnet'] = subnet

            if is_mgmt:
                data['is_mgmt'] = is_mgmt

            if is_promiscuous:
                data['is_promiscuous'] = is_promiscuous

        return self._put('/hosts/%s/networks/%s' %
                         (host_id, host_network_id), data=data)

    def update_hostnetwork(self, host_network_id, ip=None,
                           subnet_id=None, subnet=None,
                           is_mgmt=None, is_promiscuous=None,
                           raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if ip:
                data['ip'] = ip

            if subnet_id:
                data['subnet_id'] = subnet_id

            if subnet:
                data['subnet'] = subnet

            if is_mgmt:
                data['is_mgmt'] = is_mgmt

            if is_promiscuous:
                data['is_promiscuous'] = is_promiscuous

        return self._put('/host-networks/%s' % host_network_id,
                         data=data)

    def delete_host_network(self, host_id, host_network_id):
        return self._delete('/hosts/%s/networks/%s',
                            (host_id, host_network_id))

    def delete_hostnetwork(self, host_network_id):
        return self._delete('/host-networks/%s' % host_network_id)

    def get_host_state(self, host_id):
        return self._get('/hosts/%s/state' % host_id)

    def update_host_state(self, host_id, state=None,
                          percentage=None, message=None,
                          raw_data=None):
        data = {}
        if raw_data:
            data = raw_data
        else:
            if state:
                data['state'] = state

            if percentage:
                data['percentage'] = percentage

            if message:
                data['message'] = message

        return self._put('/hosts/%s/state' % host_id, date=data)

    def poweron_host(self, host_id):
        data = {}
        data['poweron'] = True

        return self._post('/hosts/%s/action' % host_id, data=data)

    def poweroff_host(self, host_id):
        data = {}
        data['poweroff'] = True

        return self._post('/hosts/%s/action' % host_id, data=data)

    def reset_host(self, host_id):
        data = {}
        data['reset'] = True

        return self._post('/hosts/%s/action' % host_id, data=data)

    def clusterhost_ready(self, clusterhost_name):
        data = {}
        data['ready'] = True

        return self._post('/clusterhosts/%s/state_internal' %
                          clusterhost_name, data=data)