Comment 9 for bug 1938927

Revision history for this message
James Vaughn (jmcvaughn) wrote :

Hi Huw,

Thanks for confirming; I've tested this and found the same. I've added the feature tag bag in as a result.

Just a final note/correction r.e. tags from the bug description. From further testing and a proper look at https://maas.io/docs/snap/2.9/ui/tags-and-annotations#heading--name-tags, MAAS only supports alphanumeric characters, underscores and dashes. Additionally, MAAS *does* appear to support partial tag matches from my testing.

If somebody wants to use tags to achieve a similar result in the interim, the following is primitive and provided with no guarantees. It takes the simply takes the power address and replaces '.' with '_', creates the tag, then assigns it to the machine. There are likely better ways to do this but the following was certainly the easiest :)

```
#!/usr/bin/env python3

maas_user = 'replaceme'

import json
import subprocess
import sys

def create_tag(tag):
    try:
        subprocess.run(['maas', maas_user, 'tags', 'create',
            'name=' + tag], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        sys.exit('ERROR: Failed to create tag "{}":\n{}'.format(tag, e.stderr))

def tag_machine(machine, tag):
    try:
        subprocess.run(['maas', maas_user, 'tag', 'update-nodes', tag, 'add='
            + machine], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        sys.exit('ERROR: Failed to add tag "{}" to machine {}'.format(tag,
            machine))

def get_power_params():
    try:
        cmd = subprocess.run(['maas', maas_user, 'machines',
            'power-parameters'], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        sys.exit('ERROR: Failed to get power-parameters:\n' + e.stderr)
    return json.loads(cmd.stdout)

def main():
    power_params = get_power_params()

    for machine in power_params:
        if 'power_address' in power_params[machine]:
            tag = power_params[machine]['power_address'].replace('.', '_')
            create_tag(tag)
            tag_machine(machine, tag)

if __name__ == '__main__':
    main()
```