#Python modules - chaining commands

1 messages · Page 1 of 1 (latest)

cloud tide
#

After reading https://blog.matiaspan.dev/posts/exploring-dagger-building-a-ci-cd-pipeline-for-iac/ I'd like to recreate parts of this in Python for a demonstration: in particular chaining methods. I've got (I think) most of what I need, but attempting to chain commands fails with a clear error "unknown command <subcommand> for 'dagger call <first command that starts with_>" so I'm guessing I don't have this bit from the blog right:

There are 2 public functions (WithAwsCredentials and WithPulumiToken) that set the credentials for what we want to do and need to be called before our operation. See how they return a reference to the module itself, in Dagger-lang this means that in order for something to happen they require a subcommand (like up).

#

@brazen robin (Sorry for the ping, no urgency here, think you're the best placed person to answer this?)

#

I can send a link to repo/module if that's helpful

brazen robin
#

Hey, glad to help 🙂 I'll take a look.

brazen robin
#

I've caught up but I'll wait to see what you have and help fix it from there.

#

Clarification from Python SDK may be in order: For the module's main functions, you can either make them top level functions or methods of an object with the same name as the module (like in Go). In this case, since you want to preserve state between functions, you don't have to create a second object type, you can just use the main object type. However, in this case, all fields need a default because as of now, dagger constructs the main object only without arguments (i.e., Pulumi()):

import dagger
from dagger.mod import function, field, object_type

@object_type
class Pulumi:
    aws_access_key: dagger.Secret | None = None
    aws_secret_key: dagger.Secret | None = None
    pulumi_token: dagger.Secret | None = None
    version: str = "latest"

    @function
    def with_pulumi_token(self, token: dagger.Secret) -> "Pulumi":
        self.pulumi_token = token
        return self

    @function
    def preview(self, src: dagger.Directory, stack: str) -> str:
        ...
cloud tide
#

Yep, I've added the few missing bits (-> "<module>" and return -> return self) and it's running, though returning an error __init__() is missing 1 required keyword argument - I've tried using init=False or default_factory=list but they returned errors previously, probably worth retrying now

#

Ah, I have misunderstood field! That answers another question as to why it shows up in --help unexpectedly. I don't need this to be a field, but an optional value that defaults to None

cloud tide
#

Got it working, thanks for the example above - that was exactly what I needed!