@@ -546,7 +546,16 @@ def configure(self):
546
546
regions = [r ["id" ] for r in self ._do_get_request ("/regions" )["data" ]]
547
547
types = [t ["id" ] for t in self ._do_get_request ("/linode/types" )["data" ]]
548
548
images = [i ["id" ] for i in self ._do_get_request ("/images" )["data" ]]
549
- auth_users = [u ["username" ] for u in self ._do_get_request ("/account/users" , token = config ["token" ])["data" ] if "ssh_keys" in u ]
549
+
550
+ is_full_access = self ._check_full_access (config ["token" ])
551
+
552
+ auth_users = []
553
+
554
+ if is_full_access :
555
+ auth_users = [u ["username" ] for u in self ._do_get_request (
556
+ "/account/users" ,
557
+ exit_on_error = False ,
558
+ token = config ["token" ])["data" ] if "ssh_keys" in u ]
550
559
551
560
# get the preferred things
552
561
config ["region" ] = self ._default_thing_input (
@@ -679,6 +688,19 @@ def _do_get_request(self, url, token=None, exit_on_error=True):
679
688
requests .get , url , token = token , exit_on_error = exit_on_error
680
689
)
681
690
691
+ @staticmethod
692
+ def _handle_response_status (response , exit_on_error = None ):
693
+ if 199 < response .status_code < 300 :
694
+ return
695
+
696
+ print (
697
+ "Could not contact {} - Error: {}" .format (
698
+ response .url , response .status_code
699
+ )
700
+ )
701
+ if exit_on_error :
702
+ sys .exit (4 )
703
+
682
704
def _do_request (self , method , url , token = None , exit_on_error = None , body = None ):
683
705
"""
684
706
Does helper requests during configuration
@@ -691,17 +713,22 @@ def _do_request(self, method, url, token=None, exit_on_error=None, body=None):
691
713
692
714
result = method (self .base_url + url , headers = headers , json = body )
693
715
694
- if not 199 < result .status_code < 300 :
695
- print (
696
- "Could not contact {} - Error: {}" .format (
697
- self .base_url + url , result .status_code
698
- )
699
- )
700
- if exit_on_error :
701
- sys .exit (4 )
716
+ self ._handle_response_status (result , exit_on_error = exit_on_error )
702
717
703
718
return result .json ()
704
719
720
+ def _check_full_access (self , token ):
721
+ headers = {
722
+ "Authorization" : "Bearer {}" .format (token ),
723
+ "Content-Type" : "application/json"
724
+ }
725
+
726
+ result = requests .get (self .base_url + "/profile/grants" , headers = headers )
727
+
728
+ self ._handle_response_status (result , exit_on_error = True )
729
+
730
+ return result .status_code == 204
731
+
705
732
def _handle_no_default_user (self ):
706
733
"""
707
734
Handle the case that there is no default user in the config
0 commit comments