Skip to content

Commit

Permalink
Merge pull request juju#18460 from gfouillet/v4/dqlite/resources/stat…
Browse files Browse the repository at this point in the history
…e-6479-manage-rows

juju#18460

Implements function to delete resources.

Include service and state layer.

* DeleteUnitResource removes the links between a unit and its linked resources
* DeleteApplicationResource removes all resources from an application, providing their are not linked to any unit or stored data.

> [!NOTE]
> Adding resources will be done in another PR, directly in the CreateApplication transaction

<!-- Why this change is needed and what it does. -->

## Checklist

<!-- If an item is not applicable, use `~strikethrough~`. -->

- [x] Code style: imports ordered, good names, simple structure, etc
- [x] Comments saying why design decisions were made
- [x] Go unit tests, with comments saying what you're testing
~- [ ] [Integration tests](https://github.com/juju/juju/tree/main/tests), with comments saying what you're testing~
~- [ ] [doc.go](https://discourse.charmhub.io/t/readme-in-packages/451) added or updated in changed packages~

## QA steps

None. Unit test should pass and cover usecases.

## Documentation changes

None

## Links

**Jira card:** JUJU-[6479](https://warthogs.atlassian.net/browse/JUJU-6479)
  • Loading branch information
jujubot authored Dec 9, 2024
2 parents bc23d5c + 8e228bd commit 0588170
Show file tree
Hide file tree
Showing 13 changed files with 823 additions and 39 deletions.
1 change: 1 addition & 0 deletions api/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (*ImportSuite) TestImports(c *gc.C) {
"internal/charm/assumes",
"internal/charm/hooks",
"internal/charm/resource",
"internal/errors",
"internal/featureflag",
"internal/http",
"internal/logger",
Expand Down
1 change: 1 addition & 0 deletions core/migration/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (*ImportTest) TestImports(c *gc.C) {
"core/network",
"core/resource",
"internal/charm/resource",
"internal/errors",
"internal/logger",
"internal/uuid",
})
Expand Down
46 changes: 46 additions & 0 deletions core/resource/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package resource

import (
"github.com/juju/juju/internal/errors"
)

// These are the valid resource states.
const (
// StateAvailable represents a resource which will be used by any units at
// this point in time
StateAvailable State = "available"

// StatePotential indicates there is a different revision of the resource
// available in a repository. Used to let users know a resource can be
// upgraded.
StatePotential State = "potential"
)

// State identifies the resource state in an application
type State string

// ParseState converts the provided string into an State.
// If it is not a known state then an error is returned.
func ParseState(value string) (State, error) {
state := State(value)
return state, state.Validate()
}

// String returns the printable representation of the state.
func (o State) String() string {
return string(o)
}

// Validate ensures that the state is correct.
func (o State) Validate() error {
if _, ok := map[State]bool{
StateAvailable: true,
StatePotential: true,
}[o]; !ok {
return errors.Errorf("state %q invalid", o)
}
return nil
}
54 changes: 54 additions & 0 deletions core/resource/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package resource

import (
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)

type StateSuite struct {
testing.IsolationSuite
}

var _ = gc.Suite(&StateSuite{})

func (StateSuite) TestParseStateKnown(c *gc.C) {
recognized := map[string]State{
"potential": StatePotential,
"available": StateAvailable,
}
for value, expected := range recognized {
state, err := ParseState(value)

c.Check(err, jc.ErrorIsNil)
c.Check(state, gc.Equals, expected)
}
}

func (StateSuite) TestParseStateUnknown(c *gc.C) {
_, err := ParseState("<invalid>")

c.Check(err, gc.ErrorMatches, `.*state "<invalid>" invalid.*`)
}

func (StateSuite) TestValidateKnown(c *gc.C) {
recognized := []State{
StatePotential,
StateAvailable,
}
for _, state := range recognized {
err := state.Validate()

c.Check(err, jc.ErrorIsNil)
}
}

func (StateSuite) TestValidateUnknown(c *gc.C) {
var state State
err := state.Validate()

c.Check(err, gc.ErrorMatches, `.*state "" invalid.*`)
}
1 change: 1 addition & 0 deletions core/watcher/eventsource/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (*ImportTest) TestImports(c *gc.C) {
"core/status",
"core/watcher",
"internal/charm/resource",
"internal/errors",
"internal/logger",
"internal/uuid",
})
Expand Down
1 change: 1 addition & 0 deletions core/watcher/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (s *ImportTest) TestImports(c *gc.C) {
"core/secrets",
"core/status",
"internal/charm/resource",
"internal/errors",
"internal/logger",
"internal/uuid",
})
Expand Down
21 changes: 20 additions & 1 deletion domain/resource/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

package errors

import "github.com/juju/juju/internal/errors"
import (
"github.com/juju/juju/internal/errors"
)

const (
// ApplicationIDNotValid describes an error when the application ID is
// not valid.
ApplicationIDNotValid = errors.ConstError("application ID not valid")

// ApplicationNotFound describes an error that occurs when the application
// being operated on does not exist.
ApplicationNotFound = errors.ConstError("application not found")
Expand All @@ -29,4 +35,17 @@ const (
// UnitNotFound describes an error that occurs when the unit being operated on
// does not exist.
UnitNotFound = errors.ConstError("unit not found")

// UnitUUIDNotValid describes an error when the unit UUID is
// not valid.
UnitUUIDNotValid = errors.ConstError("unit UUID not valid")

// ResourceStateNotValid describes an error where the resource state is not
// valid.
ResourceStateNotValid = errors.ConstError("resource state not valid")

// InvalidCleanUpState describes an error where the application state is
// during cleanup. It means that application dependencies are deleted in
// an incorrect order.
InvalidCleanUpState = errors.ConstError("invalid cleanup state")
)
76 changes: 76 additions & 0 deletions domain/resource/service/package_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0588170

Please sign in to comment.