<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2015, <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: cs_project
short_description: Manages projects on Apache CloudStack based clouds.
description:
    - Create, update, suspend, activate and remove projects.
version_added: '2.0'
author: '"<NAME> (@resmo)" <<EMAIL>>'
options:
  name:
    description:
      - Name of the project.
    required: true
  displaytext:
    description:
      - Displaytext of the project.
      - If not specified, C(name) will be used as displaytext.
    required: false
    default: null
  state:
    description:
      - State of the project.
    required: false
    default: 'present'
    choices: [ 'present', 'absent', 'active', 'suspended' ]
  domain:
    description:
      - Domain the project is related to.
    required: false
    default: null
  account:
    description:
      - Account the project is related to.
    required: false
    default: null
  poll_async:
    description:
      - Poll async jobs until job has finished.
    required: false
    default: true
extends_documentation_fragment: cloudstack
'''

EXAMPLES = '''
# Create a project
- local_action:
    module: cs_project
    name: web

# Rename a project
- local_action:
    module: cs_project
    name: web
    displaytext: my web project

# Suspend an existing project
- local_action:
    module: cs_project
    name: web
    state: suspended

# Activate an existing project
- local_action:
    module: cs_project
    name: web
    state: active

# Remove a project
- local_action:
    module: cs_project
    name: web
    state: absent
'''

RETURN = '''
---
id:
  description: ID of the project.
  returned: success
  type: string
  sample: 04589590-ac63-4ffc-93f5-b698b8ac38b6
name:
  description: Name of the project.
  returned: success
  type: string
  sample: web project
displaytext:
  description: Display text of the project.
  returned: success
  type: string
  sample: web project
state:
  description: State of the project.
  returned: success
  type: string
  sample: Active
domain:
  description: Domain the project is related to.
  returned: success
  type: string
  sample: example domain
account:
  description: Account the project is related to.
  returned: success
  type: string
  sample: example account
tags:
  description: List of resource tags associated with the project.
  returned: success
  type: dict
  sample: '[ { "key": "foo", "value": "bar" } ]'
'''

try:
    from cs import CloudStack, CloudStackException, read_config
    has_lib_cs = True
except ImportError:
    has_lib_cs = False

# import cloudstack common
from ansible.module_utils.cloudstack import *


class AnsibleCloudStackProject(AnsibleCloudStack):

    def __init__(self, module):
        AnsibleCloudStack.__init__(self, module)
        self.project = None


    def get_displaytext(self):
        displaytext = self.module.params.get('displaytext')
        if not displaytext:
            displaytext = self.module.params.get('name')
        return displaytext


    def get_project(self):
        if not self.project:
            project = self.module.params.get('name')

            args                = {}
            args['listall']     = True
            args['account']     = self.get_account(key='name')
            args['domainid']    = self.get_domain(key='id')

            projects = self.cs.listProjects(**args)
            if projects:
                for p in projects['project']:
                    if project in [ p['name'], p['id']]:
                        self.project = p
                        break
        return self.project


    def present_project(self):
        project = self.get_project()
        if not project:
            project = self.create_project(project)
        else:
            project = self.update_project(project)
        return project


    def update_project(self, project):
        args                = {}
        args['id']          = project['id']
        args['displaytext'] = self.get_displaytext()

        if self._has_changed(args, project):
            self.result['changed'] = True
            if not self.module.check_mode:
                project = self.cs.updateProject(**args)

                if 'errortext' in project:
                    self.module.fail_json(msg="Failed: '%s'" % project['errortext'])

                poll_async = self.module.params.get('poll_async')<commit_msg>cloudstack: fix project name must not be case sensitiv<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2015, <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: cs_project
short_description: Manages projects on Apache CloudStack based clouds.
description:
    - Create, update, suspend, activate and remove projects.
version_added: '2.0'
author: '"<NAME> (@resmo)" <<EMAIL>>'
options:
  name:
    description:
      - Name of the project.
    required: true
  displaytext:
    description:
      - Displaytext of the project.
      - If not specified, C(name) will be used as displaytext.
    required: false
    default: null
  state:
    description:
      - State of the project.
    required: false
    default: 'present'
    choices: [ 'present', 'absent', 'active', 'suspended' ]
  domain:
    description:
      - Domain the project is related to.
    required: false
    default: null
  account:
    description:
      - Account the project is related to.
    required: false
    default: null
  poll_async:
    description:
      - Poll async jobs until job has finished.
    required: false
    default: true
extends_documentation_fragment: cloudstack
'''

EXAMPLES = '''
# Create a project
- local_action:
    module: cs_project
    name: web

# Rename a project
- local_action:
    module: cs_project
    name: web
    displaytext: my web project

# Suspend an existing project
- local_action:
    module: cs_project
    name: web
    state: suspended

# Activate an existing project
- local_action:
    module: cs_project
    name: web
    state: active

# Remove a project
- local_action:
    module: cs_project
    name: web
    state: absent
'''

RETURN = '''
---
id:
  description: ID of the project.
  returned: success
  type: string
  sample: 04589590-ac63-4ffc-93f5-b698b8ac38b6
name:
  description: Name of the project.
  returned: success
  type: string
  sample: web project
displaytext:
  description: Display text of the project.
  returned: success
  type: string
  sample: web project
state:
  description: State of the project.
  returned: success
  type: string
  sample: Active
domain:
  description: Domain the project is related to.
  returned: success
  type: string
  sample: example domain
account:
  description: Account the project is related to.
  returned: success
  type: string
  sample: example account
tags:
  description: List of resource tags associated with the project.
  returned: success
  type: dict
  sample: '[ { "key": "foo", "value": "bar" } ]'
'''

try:
    from cs import CloudStack, CloudStackException, read_config
    has_lib_cs = True
except ImportError:
    has_lib_cs = False

# import cloudstack common
from ansible.module_utils.cloudstack import *


class AnsibleCloudStackProject(AnsibleCloudStack):

    def __init__(self, module):
        AnsibleCloudStack.__init__(self, module)
        self.project = None


    def get_displaytext(self):
        displaytext = self.module.params.get('displaytext')
        if not displaytext:
            displaytext = self.module.params.get('name')
        return displaytext


    def get_project(self):
        if not self.project:
            project = self.module.params.get('name')

            args                = {}
            args['listall']     = True
            args['account']     = self.get_account(key='name')
            args['domainid']    = self.get_domain(key='id')

            projects = self.cs.listProjects(**args)
            if projects:
                for p in projects['project']:
                    if project.lower() in [ p['name'].lower(), p['id']]:
                        self.project = p
                        break
        return self.project


    def present_project(self):
        project = self.get_project()
        if not project:
            project = self.create_project(project)
        else:
            project = self.update_project(project)
        return project


    def update_project(self, project):
        args                = {}
        args['id']          = project['id']
        args['displaytext'] = self.get_displaytext()

        if self._has_changed(args, project):
            self.result['changed'] = True
            if not self.module.check_mode:
                project = self.cs.updateProject(**args)

                if 'errortext' in project:
                    self.module.fail_json(msg="Failed: '%s'" % project['errortext'])

                poll_async = self.module.params.get('poll_async')
{'fraction_non_alphanumeric': 0.09323940905977889, 'fraction_numerical': 0.005674591527247823, 'mean_word_length': 2.577878893944697, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2649635', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2838, 'n_words': 971}