cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
cfsp_user.py
Go to the documentation of this file.
1 # *****************************************************************************
2 # * cloudFPGA
3 # * Copyright 2016 -- 2022 IBM Corporation
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # *----------------------------------------------------------------------------
16 
17 """
18 Usage:
19  cfsp user (load | show) [-c CFGFILE]
20 
21 commands:
22  load Load credentials from a file
23  show Show the credentials of a file
24 
25 options:
26  -c --config CFGFILE Specify the configfile that rsnapshot should use
27  [default: ./user.json]
28 """
29 from docopt import docopt
30 import json
31 import cfsp_globals
32 import sys
33 import os
34 
35 
38 
39 class cFuser:
40  """A user in the cloudFPGA world"""
41 
42  def __init__(self, CFGFILE):
43  username, password, project = load_user_credentials(CFGFILE)
44  self.projectproject = project
45  self.usernameusername = username
46  self.passwordpassword = password
47  self.CFGFILECFGFILE = CFGFILE
48 
49 
50  def get_auth_string(self, with_project=False):
51  if with_project:
52  s = "username={0}&password={1}&project_name={2}".format(self.usernameusername, self.passwordpassword, self.projectproject)
53  return requests.utils.requote_uri(s)
54  else:
55  s = "username={0}&password={1}".format(self.usernameusername, self.passwordpassword)
56  return requests.utils.requote_uri(s)
57 
58  def set_project(self, new_project):
59  """
60  This method should be used if the cloudFPGA quota name that should be used for this handle is different from the
61  default or credentials file
62  :param new_project:
63  :return: nothing
64  """
65  self.projectproject = new_project
66 
67  def print_credentials(self):
68  print("File : " + self.CFGFILECFGFILE)
69  print("User : " + self.usernameusername)
70  print("Password : " + self.passwordpassword)
71  print("Project : " + self.projectproject)
72 
73 
74 
75 def main(args):
76  if (len(args['<args>']) != 1):
77  print("ERROR: invalid arguments provided in 'cfsp user' command. Aborting...")
78  exit(print(__doc__))
79 
80  if args['<args>'][0] == 'load':
81  if (len(args['<args>']) == 1):
82  user=cFuser(args['--config'])
83  else:
84  print("ERROR: invalid arguments provided in cfsp user load. Aborting...")
85  sys.exit(-1)
86  if args['--username'] != 'username_example':
87  cfsp_globals.__cfsp_username__ = user.username = args['--username']
88  else:
89  cfsp_globals.__cfsp_username__ = user.username
90  if args['--password'] != 'password_example':
91  cfsp_globals.__cfsp_password__ = user.password = args['--password']
92  else:
93  cfsp_globals.__cfsp_password__ = user.password
94  if args['--project'] != 'project_example':
95  cfsp_globals.__cfsp_project__ = user.project = args['--project']
96  else:
97  cfsp_globals.__cfsp_project__ = user.project
98  write_user_credentials(args['--config'])
99  elif args['<args>'][0] == 'show':
100  if (len(args['<args>']) == 1):
101  print_user_credentials_from_file(args['--config'])
102  else:
103  print("ERROR: invalid arguments provided in cfsp user show. Aborting...")
104  sys.exit(-1)
105  else:
106  exit(print("ERROR: invalid command provided in cfsp user. Type 'cfsp help user' to get a list of supported commands. Aborting..."))
107 
108 def load_user_credentials(json_file):
109  """returns username, password, and project from a JSON file"""
110  username = ""
111  password = ""
112  project = ""
113 
114  try:
115  if os.path.exists(json_file):
116  with open(json_file, 'r') as infile:
117  data = json.load(infile)
118  username = data['credentials']['username']
119  password = data['credentials']['password']
120  if 'project' in data:
121  project = data['project']
122  else:
123  project = cfsp_globals.__openstack_user_template__['project']
124  else:
125  print("INFO: Creating new user credentials file : {}\n".format(os.path.abspath(json_file)))
126  write_user_credentials(json_file)
127  except Exception as e:
128  exit(print(e))
129  return username, password, project
130 
131 
132 def write_user_credentials(json_file):
133  """writes credentials to a JSON file"""
134  __cfsp_template__ = {}
135  __cfsp_template__['credentials'] = {}
136  __cfsp_template__['credentials']['username'] = cfsp_globals.__cfsp_username__
137  __cfsp_template__['credentials']['password'] = cfsp_globals.__cfsp_password__
138  __cfsp_template__['project'] = cfsp_globals.__cfsp_project__
139  try:
140  with open(json_file, 'w') as outfile:
141  json.dump(__cfsp_template__, outfile)
142  except Exception as e:
143  print(e)
144 
146  try:
147  with open(json_file, 'r') as infile:
148  data = json.load(infile)
149  username = data['credentials']['username']
150  password = data['credentials']['password']
151  if 'project' in data:
152  project = data['project']
153  else:
154  project = cfsp_globals.__openstack_user_template__['project']
155  print("User : " + username)
156  print("Password : " + password)
157  print("Project : " + project)
158  return 0
159  except Exception as e:
160  print(e)
161  print("No credentials file found : "+ json_file)
162  sys.exit(1)
163 
164 
165 
166 
167 if __name__ == '__main__':
168  main()
User functions.
Definition: cfsp_user.py:39
def get_auth_string(self, with_project=False)
Definition: cfsp_user.py:50
def print_credentials(self)
Definition: cfsp_user.py:67
def set_project(self, new_project)
Definition: cfsp_user.py:58
def __init__(self, CFGFILE)
Definition: cfsp_user.py:42
def main(args)
Definition: cfsp_user.py:75
def load_user_credentials(json_file)
Definition: cfsp_user.py:108
def write_user_credentials(json_file)
Definition: cfsp_user.py:132
def print_user_credentials_from_file(json_file)
Definition: cfsp_user.py:145