mirror of
https://github.com/Hopiu/portainer-cli.git
synced 2026-03-22 17:00:23 +00:00
Fix return + add errors
This commit is contained in:
parent
2482183ad0
commit
cb2b814c80
1 changed files with 16 additions and 12 deletions
|
|
@ -213,26 +213,30 @@ class PortainerCLI(object):
|
|||
|
||||
def get_stacks(self):
|
||||
stack_url = 'stacks'
|
||||
result = self.request(
|
||||
stack_url,
|
||||
self.METHOD_GET
|
||||
).json()
|
||||
return = self.request(stack_url, self.METHOD_GET).json()
|
||||
|
||||
def get_stack_by_id(self, stack_id, endpoint_id):
|
||||
stack_url = 'stacks/{}?endpointId={}'.format(
|
||||
stack_id,
|
||||
endpoint_id,
|
||||
)
|
||||
return self.request(stack_url).json()
|
||||
stack = self.request(stack_url).json()
|
||||
if not stack:
|
||||
logger.error('Stack with id={} does not exist'.format(stack_id))
|
||||
sys.exit(1)
|
||||
return stack
|
||||
|
||||
def get_stack_by_name(self, stack_name, endpoint_id):
|
||||
def get_stack_by_name(self, stack_name, endpoint_id, mandatory=False):
|
||||
result = self.get_stacks()
|
||||
if not result:
|
||||
if result:
|
||||
for stack in result:
|
||||
if stack['Name'] == stack_name and stack['EndpointId'] == endpoint_id:
|
||||
return stack
|
||||
if mandatory:
|
||||
logger.error('Stack with name={} and endpoint_id={} does not exist'.format(stack_name, endpoint_id))
|
||||
sys.exit(1)
|
||||
else:
|
||||
return None
|
||||
for stack in result:
|
||||
if stack['Name'] == stack_name and stack['EndpointId'] == endpoint_id:
|
||||
return stack
|
||||
return None
|
||||
|
||||
# Retrieve the stack if. -1 if the stack does not exist
|
||||
@plac.annotations(
|
||||
|
|
@ -340,7 +344,7 @@ class PortainerCLI(object):
|
|||
if stack_id:
|
||||
stack = self.get_stack_by_id(stack_id, endpoint_id)
|
||||
elif stack_name:
|
||||
stack = self.get_stack_by_name(stack_name, endpoint_id)
|
||||
stack = self.get_stack_by_name(stack_name, endpoint_id, True)
|
||||
else:
|
||||
logger.error('Please provide either stack_name or stack_id')
|
||||
sys.exit(1)
|
||||
|
|
|
|||
Loading…
Reference in a new issue