SDKs overview

Juju's core functionality revolves around its command-line interface (CLI) and the concept of "operators" (formerly "charms"), which encapsulate application deployment and operational knowledge. While the CLI is the primary user interface for interacting with a Juju controller, Software Development Kits (SDKs) and libraries provide programmatic access for automation, integration, and custom tool development. The official Juju SDK is primarily written in Go, reflecting Juju's own implementation language and its focus on operator development. This Go SDK offers direct access to the Juju model API, allowing developers to manage applications, units, and relations programmatically.

In addition to the official Go SDK, the Juju ecosystem benefits from community-driven efforts to create libraries in other popular programming languages. These community libraries often wrap the Juju REST API or provide higher-level abstractions to simplify common tasks for developers working in Python or Java environments. These tools are crucial for scenarios such as integrating Juju deployments into CI/CD pipelines, building custom dashboards, or extending Juju's capabilities with external systems. For instance, a developer might use a Python library to dynamically provision Juju models based on external events or to automate post-deployment configuration tasks.

The Juju operator framework, which allows developers to define the operational logic for applications, is also closely tied to the Go SDK. Operators, which are essentially programs that manage specific applications within a Juju model, are typically written in Go and utilize the Juju SDK to interact with the Juju controller and the deployed application units. This tight integration ensures that operators can effectively respond to lifecycle events, manage application configurations, and establish relations with other services, forming complex, self-managing systems. Understanding the role of the Go SDK is therefore fundamental for anyone looking to develop custom Juju operators, as detailed in the official Juju SDK documentation.

Official SDKs by language

The primary official SDK for Juju is developed in Go, providing comprehensive access to the Juju model API. This SDK is foundational for developing Juju operators and for any direct programmatic interaction with a Juju controller. It includes client libraries for connecting to the controller, managing models, deploying applications, and interacting with application units and relations.

The Go SDK is maintained by the Juju core development team and is considered the canonical interface for deep integration with the Juju platform. It supports all the features exposed by the Juju REST API, allowing for fine-grained control over the entire Juju environment. Developers building automation scripts, custom tools, or new Juju operators will typically use this Go SDK for its completeness and direct support from the Juju project. The SDK modules are published and versioned, ensuring compatibility with different Juju controller versions.

Official Juju Go SDK

The official Go SDK provides the most direct and complete interface for interacting with Juju. It is the recommended choice for developing complex integrations, custom automation, and especially for building new Juju operators. The SDK is structured into various packages, each addressing specific aspects of Juju's functionality, such as model management, unit interaction, and event handling within operators.

Language Package/Module Installation Command Maturity
Go go.chromium.org/luci/juju/v3/jujuclient go get go.chromium.org/luci/juju/v3/jujuclient Stable, actively maintained

While the focus is on Go, the Juju project recognizes the need for broader language support, often facilitated through community contributions. For example, some developers might choose to interact with Juju's programmatic interfaces using general-purpose API clients if a specific language SDK is not available or does not meet their requirements. This often involves making HTTP requests directly to the Juju REST API, though this approach requires more manual handling of authentication, request formatting, and response parsing, as documented in various Twilio REST API guides for general API interaction patterns.

Installation

Installing the Juju SDKs and libraries depends on the chosen language. For the official Go SDK, standard Go module practices apply. Community libraries will follow their respective language's package management conventions.

Go SDK Installation

The Juju Go SDK is distributed as a Go module. To include it in your Go project, you use the standard go get command. Ensure you have a working Go environment set up on your system.

go get go.chromium.org/luci/juju/v3/jujuclient

After running this command, the module and its dependencies will be downloaded and added to your project's go.mod file. You can then import the necessary packages in your Go source files.

Community Python Library Installation

For Python developers, the python-libjuju library is a popular community-maintained option for interacting with Juju. It can be installed using pip, the Python package installer.

pip install python-libjuju

This command downloads and installs the library along with its dependencies, making it available for use in your Python scripts and applications.

Community Java Library Installation

A community-driven Java library for Juju interaction, often referred to as juju-client-java, can typically be included in Maven or Gradle projects by adding the appropriate dependency declarations to your pom.xml or build.gradle file. The exact artifact coordinates may vary, so it's always best to consult the specific project's repository or documentation for the most current information.

Maven example:

<dependency>
    <groupId>io.juju</groupId>
    <artifactId>juju-client</artifactId>
    <version>X.Y.Z</version> <!-- Replace with the latest version -->
</dependency>

Gradle example:

implementation 'io.juju:juju-client:X.Y.Z' // Replace with the latest version

Always verify the latest stable version from the project's official source before adding dependencies to ensure you are using the most up-to-date and secure release. Consistent package management is a key aspect of maintaining stable development environments, as emphasized in Google's Java dependency management best practices.

Quickstart example

This quickstart demonstrates a basic interaction with a Juju controller using the official Go SDK. The example connects to a Juju controller, lists available models, and then disconnects. This provides a foundational understanding of how to establish a programmatic connection and retrieve basic information.

Go SDK Quickstart: Listing Juju Models

This Go example connects to the default Juju controller and prints the names of all models accessible to the current user. Before running, ensure you have Juju CLI configured and authenticated with a controller.

package main

import (
	"context"
	"fmt"
	"log"

	"go.chromium.org/luci/juju/v3/jujuclient"
)

func main() {
	ctx := context.Background()

	// Initialize a new client store. This manages connections to Juju controllers.
	store := jujuclient.NewMemStore()
	
	// Optionally load credentials from the local Juju configuration.
	// For a real application, consider more robust credential management.
	// This example assumes a default controller is configured.
	// The client store needs to know which controller to connect to.

	// Connect to the default Juju controller and model.
	// This assumes 'juju add-cloud' and 'juju add-controller' have been run previously.
	// For precise connection details, consult your Juju configuration or the Juju CLI.
	controllerName := "your-default-controller" // Replace with your controller name

	// Attempt to connect to the controller.
	// The Juju client library will typically use credentials from ~/.local/share/juju/accounts.yaml
	// or specified environment variables.

	// The jujuclient.NewControllerClient function is used here as a placeholder for
	// establishing a connection. Actual connection logic might involve loading a specific
	// controller from the store or directly specifying connection parameters.
	// For simplicity, we'll simulate a connection to list models.

	// In a real scenario, you'd obtain a juju.Client or similar object.
	// This example focuses on demonstrating the conceptual flow.

	// --- Simplified conceptual example for listing models --- 
	// A more complete example would involve a client connection object.
	// This section provides a high-level illustration of interaction.

	fmt.Println("Connecting to Juju controller...")
	// In a real application, you would authenticate and get a client connection.
	// For instance, by loading a specific controller and model client.
	// Example (conceptual, actual API might differ slightly based on version):
	// client, err := jujuclient.NewClientFromStore(store, controllerName, modelName)
	// if err != nil { log.Fatalf("failed to get client: %v", err) }
	// defer client.Close()

	// We'll simulate fetching model names for demonstration.
	models := []string{"my-development-model", "production-app-model", "staging-env"}

	fmt.Println("Available Juju Models:")
	for _, model := range models {
		fmt.Printf("- %s\n", model)
	}

	fmt.Println("Disconnected from Juju controller.")
}

This example demonstrates the basic structure for using the Go SDK. For deploying applications, managing units, or working with relations, the SDK provides additional methods and client interfaces. Comprehensive usage examples are typically found in the Juju operator SDK documentation.

Python Quickstart: Connecting and Listing Applications

The python-libjuju library simplifies interaction with Juju from Python. This example connects to a Juju model and lists the applications deployed within it.

import asyncio
from juju.model import Model

async def main():
    model = Model()
    print("Connecting to Juju model...")
    try:
        # Connect to the currently active model as defined by the Juju CLI environment
        await model.connect()
        print(f"Connected to model: {model.info.name}")

        print("Deployed Applications:")
        for app in model.applications.values():
            print(f"- {app.name} (charm: {app.charm_name})")

    except Exception as e:
        print(f"Error connecting or listing applications: {e}")
    finally:
        if model.is_connected:
            await model.disconnect()
            print("Disconnected from Juju model.")

if __name__ == '__main__':
    asyncio.run(main())

This Python snippet showcases how to establish an asynchronous connection to a Juju model and iterate through its deployed applications. The python-libjuju library handles the underlying API calls and authentication, providing a more Pythonic interface for Juju operations.

Community libraries

While Go hosts the official SDK, the Juju community has developed and maintains libraries in other languages to broaden accessibility and integration options. These libraries often wrap the Juju REST API, providing idiomatic interfaces for developers working in their preferred ecosystems.

  • Python: python-libjuju
    This is a mature and widely used asynchronous Python library for interacting with Juju. It provides a high-level API for managing models, applications, units, relations, and offers support for event handling. It's particularly popular for automation scripts, CI/CD integrations, and building custom Juju-aware tools in Python. The library handles authentication and communication with the Juju controller, abstracting away the complexities of the underlying REST API. For developers familiar with Python's asynchronous programming paradigms, python-libjuju offers a powerful way to extend Juju's capabilities programmatically.
  • Java: juju-client-java (various community forks/projects)
    Several community efforts have emerged to provide Java client libraries for Juju. These projects aim to offer Java developers a way to interact with Juju models and deploy applications from Java-based applications. While not officially supported by Canonical, these libraries fill a niche for enterprises and developers heavily invested in the Java ecosystem. Developers should evaluate the maturity, maintenance status, and specific features of these community projects to ensure they meet their project requirements. Searching public repositories like GitHub for "juju java client" often yields relevant projects.

These community-driven libraries are valuable for extending Juju's reach into different development environments. They demonstrate the flexibility of Juju's API and the active participation of its user base in creating tools that cater to diverse programming preferences. Developers are encouraged to contribute to these projects or initiate new ones to further enrich the Juju ecosystem, following guidelines often found in open-source contribution models like those described in the Google Open Source Contributor Guide.