Alexander Evseev | 8149449 | 2017-09-14 20:42:08 +0300 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of artifactory configuration |
| 4 | ======================================= |
| 5 | ''' |
| 6 | |
| 7 | |
| 8 | def __virtual__(): |
| 9 | if 'artifactory.get_license' in __salt__: |
| 10 | return 'artifactory' |
| 11 | else: |
| 12 | return False, 'Execution module "artifactory" is not loaded' |
| 13 | |
| 14 | |
| 15 | def add_license_key(name, license_key, **kwargs): |
| 16 | |
| 17 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 18 | kwargs.pop('license_key', None) |
| 19 | |
| 20 | result, old_license_data = __salt__['artifactory.get_license'](**kwargs) |
| 21 | |
| 22 | result, res_data = __salt__['artifactory.add_license']( |
| 23 | license_key, |
| 24 | **kwargs |
| 25 | ) |
| 26 | |
| 27 | # Prepare data to return |
| 28 | ret = { |
| 29 | 'name': name, |
| 30 | 'changes': {}, |
| 31 | 'result': result, |
| 32 | 'comment': '', |
| 33 | 'pchanges': {}, |
| 34 | } |
| 35 | |
| 36 | if result: |
| 37 | result, new_license_data = __salt__['artifactory.get_license'](**kwargs) |
| 38 | |
| 39 | if old_license_data != new_license_data: |
| 40 | ret['changes'] = { |
| 41 | 'old': json.dumps(old_license_data), |
| 42 | 'new': json.dumps(new_license_data), |
| 43 | } |
| 44 | ret['comment'] = res_data |
| 45 | else: |
| 46 | ret['comment'] = res_data['message'] |
| 47 | if ret['comment'] == ('License could not be installed due ' |
| 48 | 'to an error: License already exists.'): |
| 49 | ret['result'] = True |
| 50 | |
| 51 | return ret |
| 52 | |
| 53 | def configure_ldap(name, uri, base=None, enabled=True, dn_pattern=None, |
| 54 | manager_dn=None, manager_pass=None, search_subtree=True, |
| 55 | search_filter='(&(objectClass=inetOrgPerson)(uid={0}))', |
| 56 | attr_mail='mail', create_users=True, safe_search=True): |
| 57 | |
| 58 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 59 | |
| 60 | result, ldap_config_old = __salt__['artifactory.get_ldap_config'](name, **kwargs) |
| 61 | |
| 62 | result, res_data = __salt__['artifactory.set_ldap_config']( |
| 63 | name, uri, base, enabled, dn_pattern, manager_dn, manager_pass, |
| 64 | search_subtree, search_filter, attr_mail, create_users, safe_search, |
| 65 | **kwargs |
| 66 | ) |
| 67 | |
| 68 | # Prepare data to return |
| 69 | ret = { |
| 70 | 'name': name, |
| 71 | 'changes': {}, |
| 72 | 'result': result, |
| 73 | 'comment': '', |
| 74 | 'pchanges': {}, |
| 75 | } |
| 76 | |
| 77 | if result: |
| 78 | result, ldap_config_new = __salt__['artifactory.get_ldap_config'](name, **kwargs) |
| 79 | if ldap_config_old != ldap_config_new: |
| 80 | ret['changes'] = { |
| 81 | 'old': ldap_config_old, |
| 82 | 'new': ldap_config_new, |
| 83 | } |
| 84 | ret['comment'] = res_data |
| 85 | else: |
| 86 | ret['comment'] = res_data.get('errors')[0]['message'] |
| 87 | |
| 88 | return ret |
| 89 | |
| 90 | def configure_repo(name, **kwargs): |
| 91 | |
| 92 | repo_config = kwargs |
| 93 | repo_name = repo_config['key'] |
| 94 | |
| 95 | rclass = repo_config.pop('repo_type', 'local') |
| 96 | if 'rclass' not in repo_config: |
| 97 | repo_config['rclass'] = rclass |
| 98 | |
| 99 | packageType = repo_config.pop('package_type', 'generic') |
| 100 | if 'packageType' not in repo_config: |
| 101 | repo_config['packageType'] = packageType |
| 102 | |
| 103 | kwargs = __salt__['pillar.get']('artifactory:client:server') |
| 104 | |
| 105 | result, repo_config_old = __salt__['artifactory.get_repo'](repo_name, **kwargs) |
| 106 | |
| 107 | # Prepare data to return |
| 108 | ret = { |
| 109 | 'name': name, |
| 110 | 'changes': {}, |
| 111 | 'result': result, |
| 112 | 'comment': '', |
| 113 | 'pchanges': {}, |
| 114 | } |
| 115 | |
| 116 | result, res_data = __salt__['artifactory.set_repo'](repo_name, repo_config, **kwargs) |
| 117 | |
| 118 | if result: |
| 119 | result, repo_config_new = __salt__['artifactory.get_repo'](repo_name, **kwargs) |
| 120 | if repo_config_old != repo_config_new: |
| 121 | ret['changes'] = { |
| 122 | 'old': repo_config_old, |
| 123 | 'new': repo_config_new, |
| 124 | } |
| 125 | ret['comment'] = res_data |
| 126 | else: |
| 127 | ret['comment'] = res_data.get('errors')[0]['message'] |
| 128 | |
| 129 | return ret |