Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API output to Terraform format #1

Open
MrNunu opened this issue Jun 9, 2024 · 2 comments
Open

API output to Terraform format #1

MrNunu opened this issue Jun 9, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@MrNunu
Copy link

MrNunu commented Jun 9, 2024

NetBox ACI Plugin version

v0.0.5

NetBox version

v4.0.5

Feature type

Data model extension

Proposed functionality

Hi! Love this project so far. I realize this project is still very early in its development, but I've been looking for something like this for a while and now that the ground work is on the way, I'm thinking that I could perhaps contribute to make it fit my need.

I'd like to use the model as a source of truth for Terraform to provision at least a good chunk of an ACI tenant. I'm an extremely inexperienced developer, but I've been toying with your plugin to output the API calls in a format that Terraform will like.

I don't know if this is the right way to do this, but I've been modifying ACIEndpointGroupSerializer class in the serializer.py file to change the output. Obviously I'm changing ACIEndpointGroupSerializer to make it easier on me to test, but the end goal would be to have a parallel set of API calls to output in Terraform format.

For example (this isn't complete, but I'm happy enough with the result that I think it's possible. Also, note that the two additional classes were written with the help of ChatGPT):

class BooleanToYesNoField(serializers.Field):
    def to_representation(self, value):
        return 'yes' if value else 'no'

    def to_internal_value(self, data):
        if data.lower() == 'yes':
            return True
        elif data.lower() == 'no':
            return False
        else:
            raise serializers.ValidationError('Invalid value. Expected "yes" or "no".')

class BooleanToEnabledDisabledField(serializers.Field):
    def to_representation(self, value):
        return 'enabled' if value else 'disabled'

    def to_internal_value(self, data):
        if data.lower() == 'enabled':
            return True
        elif data.lower() == 'disabled':
            return False
        else:
            raise serializers.ValidationError('Invalid value. Expected "enabled" or "disabled".')

class ACIEndpointGroupSerializer(NetBoxModelSerializer):
    """Serializer for ACI Endpoint Group model in Terraform import format."""

    url = serializers.HyperlinkedIdentityField(
        view_name="plugins-api:netbox_aci_plugin-api:aciendpointgroup-detail"
    )
    application_profile_dn = serializers.CharField(read_only=True, source="aci_app_profile.name")
    relation_fv_rs_bd = serializers.CharField(read_only=True, source="aci_bridge_domain.name")
    shutdown = BooleanToYesNoField(source="admin_shutdown")
    flood_on_encap = BooleanToEnabledDisabledField(source="flood_on_encap")

    class Meta:
        model = ACIEndpointGroup
        fields: tuple = (
            "id",
            "url",
            "display",
            "name",
            "description",
            "application_profile_dn",
            "relation_fv_rs_bd",
            "description",
            "shutdown",
            "flood_on_encap",
        )             

So, a few questions:

Is this something you'd be interested in having in your plugin or should I fork it or something?
Is this the way to go about doing this? I can do the leg work of formatting the output if I have the "proper" way of doing it.

Use case

Use Netbox as a source of truth for Terraform

External dependencies

No response

@pheus
Copy link
Owner

pheus commented Jun 10, 2024

Hi, thank you so much for looking at this project and your kind words. Especially I like to thank you for providing a useful use case and even some code snippets. Very appreciated!

Your use case is totally valid. Actually, I started this project to be able to document and even automate the most relevant elements within Cisco ACI. But as you already know, it is quite early in the development process, since it is missing models and even simple features like bulk edits. (But I think, these thoughts were better be put in the discussion section.)

The idea behind using boolean fields for enabled and disabled states are for providing an easy to use API for any automation tool (being tool agnostic).
In your request, it seems quite Terraform specific, although I see some value in providing an APIC API mapping.
Let me think about it in more detail.

Would you mind sharing the code querying the plugin’s API and using the values as an input for the resource aci_application_epg?
(It may be possible to make the relevant mapping within the Terraform configuration itself.)

@pheus pheus added the enhancement New feature or request label Jun 10, 2024
@MrNunu
Copy link
Author

MrNunu commented Jun 12, 2024

Hi. I'm realizing that what I want is probably quite easily achieved through an export template, which may or may not be able to be included in the plugin. There probably isn't a need to do anything at all on your end for this. I can close the issue or you can as well. I can share the templates I create later on if that's of interest to you.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants