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

How to use XRC/claim values in DisposableRequest body #66

Closed
bjartekh opened this issue Oct 28, 2024 · 5 comments
Closed

How to use XRC/claim values in DisposableRequest body #66

bjartekh opened this issue Oct 28, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@bjartekh
Copy link

What problem are you facing?

I'm uncertain how I can get values supplied in my XRC(claim) substituted in the body of a DisposableRequest.

Here's an example of my 3 files.

The XRD:

apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: mycomposition.test.me.net
spec:
  group: test.me.net
  names:
    kind: mycomposition
    plural: mycompositions
  claimNames:
    kind: lz1Claim
    plural: lz1claim    
  versions:
  - name: v1alpha1
    served: true
    referenceable: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              TestFlag: 
                type: boolean
                default: true
              TestValue:
                type: string

My XRC (claim)

apiVersion: test.me.net/v1alpha1
kind: lz1Claim
metadata:
  name: my-test-project
spec:
  compositionRef:
    name: my-composition
  TestFlag: false
  TestValue: "myvalue"

And my composition (using provider-http, DisposableRequest)

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: my-composition
spec:
  resources:
    - name: http-request
      base: 
        apiVersion: http.crossplane.io/v1alpha2
        kind: DisposableRequest
        spec:
          providerConfigRef:
            name: http-test-config
          deletionPolicy: Orphan
          forProvider:
            body: |
              {
                "jsonParams": {
                    "TestFlag": ".spec.TestFlag",
                    "TestValue": "{{ .spec.TestValue }}"
                }
              }
            expectedResponse: .body.state == "inProgress"
            headers:
              Authorization:
                - "Basic {{ my-secret-external:crossplane-system:authorization }}"
              Content-Type:
                - application/json
            insecureSkipTLSVerify: true
            method: POST
            rollbackRetriesLimit: 1
            url: https://mytestdomain.com/testme/pipeline/execute
            waitTimeout: 5m

I see that the request is sent, but no substitution happens of the values supplied in the body.
I've tried various syntax too.

How could Crossplane help solve your problem?

Maybe you could add some examples of how this is done? (I looked at the examples provided and couldn't find any, it seems that the examples uses values hardcoded in the body).

It is not clear to me how I could use crossplane patches because the body type is a string.

@bjartekh bjartekh added the enhancement New feature or request label Oct 28, 2024
@arielsepton
Copy link
Member

Yes, the body is a string because CRDs don’t support an interface{} type, so all fields must be predefined.
To dynamically substitute values in the string body, you can use Crossplane’s function-go-templating—I recommend giving it a try!

Alternatively, you could use patches and transforms, as described in Crossplane’s patch and transform guide. For example:

- type: FromCompositeFieldPath
  fromFieldPath: spec.clusterName
  toFieldPath: spec.forProvider.tags
  transforms:
    - type: string
      string:
        type: Format
        fmt: '{"kubernetes.io/cluster/%s": "true"}'

Since this issue is about general Crossplane composition patterns rather than the specific provider, I’ll go ahead and close it. Let me know if this helps or if you have further questions!

@bjartekh
Copy link
Author

Hi again, I've checked out your examples but not been able to substitute the string body. I'll also add that I'm not super-experienced with Crossplane.

It would be very valuable if you could provide an example.

(I am also assuming that this would be of value for other than me, as one often wants to dynamically provide the values of a post body)

@arielsepton
Copy link
Member

arielsepton commented Oct 29, 2024

Hi there,
Here’s a starting example that might work:

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: my-composition
spec:
  resources:
    - name: http-request
      base: 
        apiVersion: http.crossplane.io/v1alpha2
        kind: DisposableRequest
        spec:
          providerConfigRef:
            name: http-test-config
          deletionPolicy: Orphan
          forProvider:
            headers:
              Authorization:
                - "Basic {{ my-secret-external:crossplane-system:authorization }}"
              Content-Type:
                - application/json
            insecureSkipTLSVerify: true
            method: POST
            url: https://mytestdomain.com/testme/pipeline/execute
      patches:
        - type: CombineFromComposite
          combine:
            variables:
              - fromFieldPath: spec.TestFlag
              - fromFieldPath: spec.TestValue
            strategy: string
            string:
              fmt: |
                {
                  "jsonParams": {
                    "TestFlag": "%s",
                    "TestValue": "%s"
                  }
                }
          toFieldPath: spec.forProvider.body

This is untested, but it should give you an idea of how to use patches to patch values into the body.

For more guidance, I recommend looking at the CombineFromComposite documentation and reaching out to the Crossplane Slack community—they’re very helpful, and this type of composition question is right in line with what they handle best. Let me know if this points you in the right direction!

@bjartekh
Copy link
Author

Many thanks @arielsepton ! Now I got it to work! :)

@briantopping
Copy link

As someone with intermediate experience in Crossplane but just learning provider-http, this ticket is a super valuable find. I hope that tickets like this can be structured into the documentation. This provider is super useful and I think it will get a lot more attention as it becomes easier to use. Thanks and happy new year!!

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

3 participants