BgpPeerGroupnetclab/netclab-xp@v0.2.13
TypeComposition
Referenced XRDBgpPeerGroup
YAML
Composition
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: bgppeergroups.eos.netclab.dev
  creationTimestamp: null
spec:
  compositeTypeRef:
    apiVersion: eos.netclab.dev/v1alpha1
    kind: BgpPeerGroup
  mode: Pipeline
  pipeline:
    - step: environmentConfigs
      functionRef:
        name: crossplane-contrib-function-environment-configs
      input:
        apiVersion: environmentconfigs.fn.crossplane.io/v1beta1
        kind: Input
        spec:
          environmentConfigs:
            - ref:
                name: endpoint-protocols
              type: Reference
    - step: requests
      functionRef:
        name: crossplane-contrib-function-go-templating
      input:
        apiVersion: gotemplating.fn.crossplane.io/v1beta1
        inline:
          template: >
            {{- $deviceUrl := "" }}

            {{- $basicAuth := "" }}

            {{- $address := .observed.composite.resource.spec.endpoint }}

            {{- with index .context "apiextensions.crossplane.io/environment" }}
              {{- $deviceUrl = printf "%s://%s:%d" .restconf.scheme $address .restconf.port }}
            {{- end }}

            {{- $basicAuth := ( getCredentialData . "eos-creds" ).basicAuth |
            toString | b64enc }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}"
                  body: |
                    {}

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl + "/config"
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl + "/config"
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl

            {{- if hasKey .observed.composite.resource.spec "nextHopUnchanged"
            }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-nhu
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/config"
                  body: |
                    { "arista-exp-eos-bgp:next-hop-unchanged": {{ .observed.composite.resource.spec.nextHopUnchanged }} }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/arista-exp-eos-bgp:next-hop-unchanged"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl + "/arista-exp-eos-bgp:next-hop-unchanged"
            {{- end }}


            {{- if hasKey .observed.composite.resource.spec "updateSource" }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-updatesource
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/transport/config"
                  body: |
                    { "openconfig-network-instance:local-address": "{{ .observed.composite.resource.spec.updateSource }}" }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/local-address"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl + "/local-address"
            {{- end }}


            {{- if and (hasKey .observed.composite.resource.spec "bfd")
            .observed.composite.resource.spec.bfd }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-bfd
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/enable-bfd/config"
                  body: |
                    { "openconfig-network-instance:enabled": {{ .observed.composite.resource.spec.bfd }} }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/enabled"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl + "/enabled"
            {{- end }}


            {{- if and (hasKey .observed.composite.resource.spec "ebgpMultihop")
            (ge .observed.composite.resource.spec.ebgpMultihop 1) }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-ebgpmultihop
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/ebgp-multihop/config"
                  body: |
                    { "openconfig-network-instance:multihop-ttl": {{ .observed.composite.resource.spec.ebgpMultihop }} }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/multihop-ttl"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl + "/multihop-ttl"

                isRemovedCheck:
                  type: CUSTOM
                  logic: |
                    (.response.statusCode == 200
                    and .response.body."openconfig-network-instance:multihop-ttl" == 0)
                    or
                    .response.statusCode == 404
            {{- end }}


            {{- if hasKey .observed.composite.resource.spec "maximumRoutes" }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-maximumroutes
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/arista-bgp-augments:prefix-limit/config"
                  body: |
                    { "arista-bgp-augments:max-prefixes": {{ .observed.composite.resource.spec.maximumRoutes }} }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/arista-bgp-augments:max-prefixes"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl
            {{- end }}


            {{- if hasKey .observed.composite.resource.spec "sendCommunityTypes"
            }}

            ---

            apiVersion: http.crossplane.io/v1alpha2

            kind: Request

            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{ .observed.composite.resource.metadata.name }}-sendcommunitytypes
            spec:
              forProvider:
                insecureSkipTLSVerify: true
                headers:
                  Accept: ["application/yang-data+json"]
                  Authorization: ["Basic {{ $basicAuth }}"]
                payload:
                  baseUrl: "{{ $deviceUrl }}/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,BGP/bgp/peer-groups/peer-group={{ .observed.composite.resource.spec.peerGroupName }}/config"
                  body: |
                    {
                      "openconfig-network-instance:send-community-type":
                        [
                          {{- range $i, $v := .observed.composite.resource.spec.sendCommunityTypes }}
                            {{- if $i }},{{ end }}"{{ $v }}"
                          {{- end }}
                        ]
                    }

                mappings:
                - action: CREATE
                  method: PATCH
                  url: .payload.baseUrl
                  body: .payload.body

                - action: UPDATE
                  method: PUT
                  url: .payload.baseUrl
                  body: .payload.body

                - action: OBSERVE
                  url: .payload.baseUrl + "/send-community-type"

                - action: REMOVE
                  method: DELETE
                  url: .payload.baseUrl + "/send-community-type"
            {{- end }}
        kind: GoTemplate
        source: Inline
      credentials:
        - name: eos-creds
          source: Secret
          secretRef:
            name: eos-creds
            namespace: crossplane-system
    - step: auto-ready
      functionRef:
        name: crossplane-contrib-function-auto-ready