Created
May 25, 2016 16:17
-
-
Save patrick-east/ebd4e42fd80fae01a58aa503e3264ac2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # Copyright (c) 2015 Pure Storage, Inc. | |
| # All Rights Reserved. | |
| # | |
| # 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. | |
| import argparse | |
| import purestorage | |
| def create_host(array, name, iqn): | |
| print 'Creating Purity host %s with iqn %s' % (name, iqn) | |
| try: | |
| array.create_host(name, iqnlist=[iqn]) | |
| except purestorage.PureHTTPError as err: | |
| if not (err.code == 400 and 'already exists' in err.text): | |
| raise | |
| else: | |
| print 'Purity host %s already exists, ignoring...' % name | |
| def delete_host(array, name): | |
| print 'Deleting Purity host %s' % name | |
| try: | |
| array.delete_host(name) | |
| except purestorage.PureHTTPError as err: | |
| if err.code == 400: | |
| if 'Host cannot be deleted due to existing connections' in err.text: | |
| #delete connections and delete host | |
| volumes = array.list_host_connections(name) | |
| print 'removing connections on host: %s' % volumes | |
| for volume in volumes: | |
| print 'deleting volume %s' % volume["vol"] | |
| array.disconnect_host(name, volume["vol"]) | |
| array.delete_host(name) | |
| elif 'does not exist' in err.text: | |
| print 'Purity host %s does not exist, ignoring...' % name | |
| else: | |
| raise | |
| else: | |
| raise | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description='Create or delete a purity host') | |
| parser.add_argument('-c', '--create', help='Create a new purity host', action='store_true') | |
| parser.add_argument('-d', '--delete', help='Delete a purity host', action='store_true') | |
| parser.add_argument('-i', '--iqn', dest='iqn', help='IQN for the host (when creating)') | |
| parser.add_argument('-a', '--address', dest='address', required=True, | |
| help='Purity hostname or ip address') | |
| parser.add_argument('-t', '--token', dest='token', required=True, | |
| help='REST API token to be used for requests') | |
| parser.add_argument('name', help='Name of the host to be operated on') | |
| args = parser.parse_args() | |
| fa = purestorage.FlashArray(args.address, api_token=args.token) | |
| if args.create: | |
| if not args.iqn: | |
| print 'Error: iqn parameter is required for creating a host!' | |
| exit(1) | |
| create_host(fa, args.name, args.iqn) | |
| elif args.delete: | |
| delete_host(fa, args.name) | |
| print 'Finished!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment