Skip to content

Commit

Permalink
refactor: changed the storage example, fixed comments, removed debug …
Browse files Browse the repository at this point in the history
…print.
  • Loading branch information
anvial committed Jul 3, 2024
1 parent 3c59d4b commit 63b56da
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 61 deletions.
24 changes: 4 additions & 20 deletions docs/resources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,14 @@ resource "juju_application" "this" {
model = juju_model.development.name
charm {
name = "hello-kubecon"
name = "ubuntu"
channel = "edge"
revision = 14
revision = 24
series = "trusty"
}
units = 3
config = {
external-hostname = "..."
}
}
resource "juju_application" "placement_and_storage_example" {
name = "placement-example"
model = juju_model.development.name
charm {
name = "hello-kubecon"
channel = "edge"
revision = 14
series = "trusty"
}
units = 3
placement = "0,1,2"
storage = {
Expand Down Expand Up @@ -83,7 +67,7 @@ resource "juju_application" "placement_and_storage_example" {
* If the charm revision or channel are not updated, then no changes will take
place (juju does not have an "un-attach" command for resources).
- `storage` (Attributes Set) Storage used by the application. (see [below for nested schema](#nestedatt--storage))
- `storage_directives` (Map of String) Storage directives (constraints) for the juju application. The map key is the label of the storage defined by the charm, the map value is the storage directive in the form <pool>,<count>,<size>.
- `storage_directives` (Map of String) Storage directives (constraints) for the juju application. The map key is the label of the storage defined by the charm, the map value is the storage directive in the form <pool>,<count>,<size>. Changing an existing key/value pair will cause the application to be replaced. Adding a new key/value pair will add storage to the application on upgrade.
- `trust` (Boolean) Set the trust for the application.
- `units` (Number) The number of application units to deploy for the charm.

Expand Down Expand Up @@ -136,7 +120,7 @@ Read-Only:

- `count` (Number) The number of volumes.
- `label` (String) The specific storage option defined in the charm.
- `pool` (String) Name of the storage pool to use.
- `pool` (String) Name of the storage pool.
- `size` (String) The size of each volume.

## Import
Expand Down
20 changes: 2 additions & 18 deletions examples/resources/juju_application/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,14 @@ resource "juju_application" "this" {
model = juju_model.development.name

charm {
name = "hello-kubecon"
name = "ubuntu"
channel = "edge"
revision = 14
revision = 24
series = "trusty"
}

units = 3

config = {
external-hostname = "..."
}
}

resource "juju_application" "placement_and_storage_example" {
name = "placement-example"
model = juju_model.development.name
charm {
name = "hello-kubecon"
channel = "edge"
revision = 14
series = "trusty"
}

units = 3
placement = "0,1,2"

storage = {
Expand Down
23 changes: 8 additions & 15 deletions internal/juju/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,6 @@ func (c applicationsClient) CreateApplication(ctx context.Context, input *Create
return nil, err
}

// print transformedInput.storage
c.Tracef("transformedInput.storage", map[string]interface{}{"transformedInput.storage": transformedInput.storage})

applicationAPIClient := apiapplication.NewClient(conn)
if applicationAPIClient.BestAPIVersion() >= 19 {
err = c.deployFromRepository(applicationAPIClient, transformedInput)
Expand Down Expand Up @@ -782,14 +779,14 @@ func (c applicationsClient) ReadApplicationWithRetryOnNotFound(ctx context.Conte
return fmt.Errorf("ReadApplicationWithRetryOnNotFound: need %d machines, have %d", output.Units, len(machines))
}

// NOTE: Application can always have storage. However, they
// NOTE: Applications can always have storage. However, they
// will not be listed right after the application is created. So
// we need to wait for the storages to be ready. And we need to
// we need to wait for the storage to be ready. And we need to
// check if all storage constraints have pool equal "" and size equal 0
// to drop the error.
for _, storage := range output.Storage {
if storage.Pool == "" || storage.Size == 0 {
return fmt.Errorf("ReadApplicationWithRetryOnNotFound: no storages found in output")
return fmt.Errorf("ReadApplicationWithRetryOnNotFound: no storage found in output")
}
}

Expand Down Expand Up @@ -832,8 +829,8 @@ func transformToStorageConstraints(
case "filesystem":
for _, fd := range filesystemDetailsSlice {
if fd.Storage.StorageTag == storageDetails.StorageTag {
// Cut 'storage-' prefix from the storage tag and `-NUMBER` suffix
storageLabel := getStorageLabel(storageDetails)
// Cut PrefixStorage from the storage tag and `-NUMBER` suffix
storageLabel := getStorageLabel(storageDetails.StorageTag)
storageCounters[storageLabel]++
storage[storageLabel] = jujustorage.Constraints{
Pool: fd.Info.Pool,
Expand All @@ -845,7 +842,7 @@ func transformToStorageConstraints(
case "block":
for _, vd := range volumeDetailsSlice {
if vd.Storage.StorageTag == storageDetails.StorageTag {
storageLabel := getStorageLabel(storageDetails)
storageLabel := getStorageLabel(storageDetails.StorageTag)
storageCounters[storageLabel]++
storage[storageLabel] = jujustorage.Constraints{
Pool: vd.Info.Pool,
Expand All @@ -859,8 +856,8 @@ func transformToStorageConstraints(
return storage
}

func getStorageLabel(storageDetails params.StorageDetails) string {
return strings.TrimSuffix(strings.TrimPrefix(storageDetails.StorageTag, "storage-"), "-0")
func getStorageLabel(storageTag string) string {
return strings.TrimSuffix(strings.TrimPrefix(storageTag, PrefixStorage), "-0")
}

func (c applicationsClient) ReadApplication(input *ReadApplicationInput) (*ReadApplicationResponse, error) {
Expand Down Expand Up @@ -916,10 +913,6 @@ func (c applicationsClient) ReadApplication(input *ReadApplicationInput) (*ReadA
}

storages := transformToStorageConstraints(status.Storage, status.Filesystems, status.Volumes)
// Print storage to console
for k, v := range storages {
c.Tracef("StorageConstraints constraints", map[string]interface{}{"storage": k, "constraints": v})
}

allocatedMachines := set.NewStrings()
for _, v := range appStatus.Units {
Expand Down
1 change: 1 addition & 0 deletions internal/juju/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
PrefixUser = "user-"
PrefixMachine = "machine-"
PrefixApplication = "application-"
PrefixStorage = "storage-"
UnspecifiedRevision = -1
connectionTimeout = 30 * time.Second
)
Expand Down
10 changes: 6 additions & 4 deletions internal/provider/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ func (r *applicationResource) Schema(_ context.Context, _ resource.SchemaRequest
},
},
"storage_directives": schema.MapAttribute{
Description: "Storage directives (constraints) for the juju application. " +
"The map key is the label of the storage defined by the charm, " +
"the map value is the storage directive in the form <pool>,<count>,<size>.",
Description: "Storage directives (constraints) for the juju application." +
" The map key is the label of the storage defined by the charm," +
" the map value is the storage directive in the form <pool>,<count>,<size>." +
" Changing an existing key/value pair will cause the application to be replaced." +
" Adding a new key/value pair will add storage to the application on upgrade.",
ElementType: types.StringType,
Optional: true,
Validators: []validator.Map{
Expand All @@ -197,7 +199,7 @@ func (r *applicationResource) Schema(_ context.Context, _ resource.SchemaRequest
Computed: true,
},
"pool": schema.StringAttribute{
Description: "Name of the storage pool to use.",
Description: "Name of the storage pool.",
Computed: true,
},
"count": schema.Int64Attribute{
Expand Down
4 changes: 0 additions & 4 deletions internal/provider/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ func TestAcc_ResourceApplication_Updates(t *testing.T) {
appName = "hello-kubecon"
}

// trace plan
t.Log(testAccResourceApplicationUpdates(modelName, 1, true, "machinename"))
t.Log(testAccResourceApplicationUpdates(modelName, 2, true, "machinename"))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: frameworkProviderFactories,
Expand Down

0 comments on commit 63b56da

Please sign in to comment.