# Plugin examples

> Worked examples of common use cases for Cloud Management Platform (CMP) plugins

---

Site [llms.txt](/llms.txt)

---

CMP hosts a [Content Library](/docs/cmp/use/catalog/content-library/) of example plugins, which you can import from most pages where actions are created, such as Server Actions and Orchestration Actions. The source for those plugins lives in a [public GitHub repository](https://github.com/CloudBoltSoftware/cloudbolt-forge/tree/master/actions/cloudbolt_plugins).

## Add a progress message

Use this at the Pre-Create Resource trigger point in a Provision Server job. It writes the server's IP address to the job progress and log, then waits 60 seconds so the message is easy to notice.

```python
import time
from common.methods import set_progress


def run(job, server=None, **kwargs):
    msg = "In Pre-Create Resource for job #{}, the server's IP is {}".format(
        job.id, server.ip)
    set_progress(msg)

    time.sleep(60)

    return "", "", ""
```

## Change a server hostname

Use this at the Pre-Create Resource trigger point in a Provision Server job to prefix the hostname the user entered with the value of the `org_id` parameter.

```python
from common.methods import set_progress


def run(job, server=None, **kwargs):
    set_progress("In Pre-Create Resource job.id={}".format(job.id))

    # Add a prefix to the hostname of any server whose group
    # has an Organization ID parameter associated with it
    org_id = server.org_id

    if not org_id:
        prog_msg = "Not altering the hostname as no org_id was found."
        set_progress(prog_msg)
        return "", "", ""

    old_hostname = server.hostname
    server.hostname = "{}{}".format(org_id, server.hostname)
    server.save()

    prog_msg = "Prefixed hostname with Organization ID. {} -> {}".format(
        old_hostname, server.hostname)
    set_progress(prog_msg)

    return "", "", ""
```

## Use multi-value parameters

This server action updates the Users parameter on the server with a New Users action input supplied at runtime. Both the parameter and the action input must allow multiple values.

```python
from common.methods import set_progress


def run(job, server=None, **kwargs):
    users = server.users
    new_users = {{ new_users }}

    for user in new_users:
        if user not in users:
            set_progress("Adding {}".format(user))
            users.append(user)

    server.users = users
    return "", "", ""
```

## Conditionally email stakeholders

Use this at the Post-Provision trigger point of a Provision Server job. It reads the `enable_monitoring` parameter and emails the configured recipient when it is set. Configure the action to run only when the job succeeds.

```python
"""
For each successful job, if the 'enable_monitoring' parameter is set, email the configured recipient.
"""

from common.methods import set_progress
from django.conf import settings
from utilities.mail import email
from django.template.loader import render_to_string


def run(job, logger, server=None, **kwargs):
    if server and server.enable_monitoring:
        set_progress("Enable monitoring is set for this job.")

        if hasattr(settings, 'ENABLE_MONITORING_EMAIL'):
            subject = render_to_string(
                'email/enable-monitoring-subj.template',
                {'servers': [server]}
            ).strip()

            body = render_to_string(
                'email/enable-monitoring-body.template',
                {
                    'owner': job.owner,
                    'servers': [server]
                }
            )

            recipient = getattr(settings, 'ENABLE_MONITORING_EMAIL')

            # Build the context dict required by the email module
            context = {
                "subject": subject,
                "message": body,
            }

            try:
                email(recipients=[recipient], context=context)
            except Exception as err:
                logger.exception("Aborting due to email error")
                return ("FAILURE", "Aborting due to email error. ", str(err))

            set_progress("Email sent to {}".format(recipient))

        else:
            set_progress(
                "Email not sent because ENABLE_MONITORING_EMAIL is not set."
            )

    else:
        set_progress("Monitoring not enabled.")

    return "", "", ""
```

## Related

- [Structure of a plugin](/docs/cmp/use/orchestration/plugins/structure/)
- [Development tips for plugins](/docs/cmp/use/orchestration/plugins/development-tips/)
- [Content Library](/docs/cmp/use/catalog/content-library/)
