Skip to content

Commit

Permalink
Add method for deleting a repository, as well as updating description…
Browse files Browse the repository at this point in the history
… and retention settings.
  • Loading branch information
SaaldjorMike committed May 19, 2020
1 parent ea938fe commit 4acc991
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 4 deletions.
157 changes: 156 additions & 1 deletion api/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Repositories struct {

type Repository struct {
Name string
Description string
RetentionDays float64 `graphql:"timeBasedRetention"`
IngestRetentionSizeGB float64 `graphql:"ingestSizeBasedRetention"`
StorageRetentionSizeGB float64 `graphql:"storageSizeBasedRetention"`
Expand All @@ -31,7 +32,12 @@ func (r *Repositories) Get(name string) (Repository, error) {

graphqlErr := r.client.Query(&q, variables)

return q.Repository, graphqlErr
if graphqlErr != nil {
// The graphql error message is vague if the repo already exists, so add a hint.
return q.Repository, fmt.Errorf("%+v. Does the repo already exist?", graphqlErr)
}

return q.Repository, nil
}

type RepoListItem struct {
Expand Down Expand Up @@ -69,3 +75,152 @@ func (r *Repositories) Create(name string) error {

return nil
}

func (r *Repositories) Delete(name, reason string, allowDataDeletion bool) error {
existingRepo, err := r.Get(name)
if err != nil {
return err
}
safeToDelete := allowDataDeletion || existingRepo.SpaceUsed == 0
if !safeToDelete {
return fmt.Errorf("repository contains data and data deletion not allowed")
}

var m struct {
CreateRepository struct {
Type string `graphql:"__typename"`
} `graphql:"deleteSearchDomain(name: $name, deleteMessage: $reason)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"reason": graphql.String(reason),
}

err = r.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}

func (r *Repositories) UpdateTimeBasedRetention(name string, retentionInDays float64, allowDataDeletion bool) error {
existingRepo, err := r.Get(name)
if err != nil {
return err
}
safeToDelete := allowDataDeletion || existingRepo.SpaceUsed == 0

var m struct {
UpdateRetention struct {
Type string `graphql:"__typename"`
} `graphql:"updateRetention(repositoryName: $name, timeBasedRetention: $retentionInDays)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"retentionInDays": (*graphql.Float)(nil),
}
if retentionInDays > 0 {
if retentionInDays < existingRepo.RetentionDays || existingRepo.RetentionDays == 0 {
if !safeToDelete {
return fmt.Errorf("repository contains data and data deletion not allowed")
}
}
variables["retentionInDays"] = graphql.Float(retentionInDays)
}

err = r.client.Mutate(&m, variables)
if err != nil {
return err
}

return nil
}

func (r *Repositories) UpdateStorageBasedRetention(name string, storageInGB float64, allowDataDeletion bool) error {
existingRepo, err := r.Get(name)
if err != nil {
return err
}
safeToDelete := allowDataDeletion || existingRepo.SpaceUsed == 0

var m struct {
UpdateRetention struct {
Type string `graphql:"__typename"`
} `graphql:"updateRetention(repositoryName: $name, storageSizeBasedRetention: $storageInGB)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"storageInGB": (*graphql.Float)(nil),
}
if storageInGB > 0 {
if storageInGB < existingRepo.StorageRetentionSizeGB || existingRepo.StorageRetentionSizeGB == 0 {
if !safeToDelete {
return fmt.Errorf("repository contains data and data deletion not allowed")
}
}
variables["storageInGB"] = graphql.Float(storageInGB)
}

err = r.client.Mutate(&m, variables)
if err != nil {
return err
}

return nil
}

func (r *Repositories) UpdateIngestBasedRetention(name string, ingestInGB float64, allowDataDeletion bool) error {
existingRepo, err := r.Get(name)
if err != nil {
return err
}
safeToDelete := allowDataDeletion || existingRepo.SpaceUsed == 0

var m struct {
UpdateRetention struct {
Type string `graphql:"__typename"`
} `graphql:"updateRetention(repositoryName: $name, ingestSizeBasedRetention: $ingestInGB)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"ingestInGB": (*graphql.Float)(nil),
}
if ingestInGB > 0 {
if ingestInGB < existingRepo.IngestRetentionSizeGB || existingRepo.IngestRetentionSizeGB == 0 {
if !safeToDelete {
return fmt.Errorf("repository contains data and data deletion not allowed")
}
}
variables["ingestInGB"] = graphql.Float(ingestInGB)
}

err = r.client.Mutate(&m, variables)
if err != nil {
return err
}

return nil
}

func (r *Repositories) UpdateDescription(name, description string) error {
var m struct {
UpdateDescription struct {
Type string `graphql:"__typename"`
} `graphql:"updateDescriptionForSearchDomain(name: $name, newDescription: $description)"`
}

variables := map[string]interface{}{
"name": graphql.String(name),
"description": graphql.String(description),
}

err := r.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/cluster_nodes_unregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func newClusterNodesUnregisterCmd() *cobra.Command {
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
node, parseErr := strconv.ParseInt(args[0], 10, 64)
exitOnError(cmd, parseErr, "Not valid node id: %s")
exitOnError(cmd, parseErr, "Not valid node id")

client := NewApiClient(cmd)

apiError := client.ClusterNodes().Unregister(node, false)
exitOnError(cmd, apiError, "Error removing parser: %s")
exitOnError(cmd, apiError, "Error removing parser")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/parsers_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newParsersRemoveCmd() *cobra.Command {
client := NewApiClient(cmd)

apiError := client.Parsers().Remove(repo, parser)
exitOnError(cmd, apiError, "Error removing parser: %s")
exitOnError(cmd, apiError, "Error removing parser")
},
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func newReposCmd() *cobra.Command {
cmd.AddCommand(newReposShowCmd())
cmd.AddCommand(newReposListCmd())
cmd.AddCommand(newReposCreateCmd())
cmd.AddCommand(newReposUpdateCmd())
cmd.AddCommand(newReposDeleteCmd())

return cmd
}
Expand All @@ -39,6 +41,7 @@ func printRepoTable(cmd *cobra.Command, repo api.Repository) {

data := [][]string{
[]string{"Name", repo.Name},
[]string{"Description", repo.Description},
[]string{"Space Used", ByteCountDecimal(repo.SpaceUsed)},
[]string{"Ingest Retention (Size)", ByteCountDecimal(int64(repo.IngestRetentionSizeGB * 1e9))},
[]string{"Storage Retention (Size)", ByteCountDecimal(int64(repo.StorageRetentionSizeGB * 1e9))},
Expand Down
42 changes: 42 additions & 0 deletions cmd/repos_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2020 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/spf13/cobra"
)

func newReposDeleteCmd() *cobra.Command {
var allowDataDeletionFlag bool

cmd := cobra.Command{
Use: "delete [flags] <repo> \"descriptive reason for why it is being deleted\"",
Short: "Delete a repository.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
repo := args[0]
reason := args[1]

client := NewApiClient(cmd)

apiError := client.Repositories().Delete(repo, reason, allowDataDeletionFlag)
exitOnError(cmd, apiError, "error removing repository")
},
}

cmd.Flags().BoolVar(&allowDataDeletionFlag, "allow-data-deletion", false, "Allow changing retention settings for a non-empty repository")

return &cmd
}
71 changes: 71 additions & 0 deletions cmd/repos_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright © 2020 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func newReposUpdateCmd() *cobra.Command {
var allowDataDeletionFlag bool
var descriptionFlag stringPtrFlag
var retentionTimeFlag, ingestSizeBasedRetentionFlag, storageSizeBasedretentionFlag float64PtrFlag

cmd := cobra.Command{
Use: "update",
Short: "Updates the settings of a repository",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repoName := args[0]

if descriptionFlag.value == nil && retentionTimeFlag.value == nil && ingestSizeBasedRetentionFlag.value == nil && storageSizeBasedretentionFlag.value == nil {
exitOnError(cmd, fmt.Errorf("you must specify at least one flag to update"), "nothing specifed to update")
}

client := NewApiClient(cmd)
if descriptionFlag.value != nil {
err := client.Repositories().UpdateDescription(repoName, *descriptionFlag.value)
exitOnError(cmd, err, "error updating repository description")
}
if retentionTimeFlag.value != nil {
err := client.Repositories().UpdateTimeBasedRetention(repoName, *retentionTimeFlag.value, allowDataDeletionFlag)
exitOnError(cmd, err, "error updating repository retention time in days")
}
if ingestSizeBasedRetentionFlag.value != nil {
err := client.Repositories().UpdateIngestBasedRetention(repoName, *ingestSizeBasedRetentionFlag.value, allowDataDeletionFlag)
exitOnError(cmd, err, "error updating repository ingest size based retention")
}
if storageSizeBasedretentionFlag.value != nil {
err := client.Repositories().UpdateStorageBasedRetention(repoName, *storageSizeBasedretentionFlag.value, allowDataDeletionFlag)
exitOnError(cmd, err, "error updating repository storage size based retention")
}

repo, apiErr := client.Repositories().Get(repoName)
exitOnError(cmd, apiErr, "error fetching repository")
printRepoTable(cmd, repo)
fmt.Println()
},
}

cmd.Flags().BoolVar(&allowDataDeletionFlag, "allow-data-deletion", false, "Allow changing retention settings for a non-empty repository")
cmd.Flags().Var(&descriptionFlag, "description", "The description of the repository.")
cmd.Flags().Var(&retentionTimeFlag, "retention-time", "The retention time in days for the repository.")
cmd.Flags().Var(&ingestSizeBasedRetentionFlag, "ingest-size-based-retention", "The ingest size based retention for the repository.")
cmd.Flags().Var(&storageSizeBasedretentionFlag, "storage-size-based-retention", "The storage size based retention for the repository.")

return &cmd
}
26 changes: 26 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/url"
"os"
"strconv"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -108,3 +109,28 @@ func (sf *urlPtrFlag) String() string {
func (sf *urlPtrFlag) Type() string {
return "url"
}

type float64PtrFlag struct {
value *float64
}

func (sf *float64PtrFlag) Set(v string) error {
var val float64
val, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
sf.value = &val
return nil
}

func (sf *float64PtrFlag) String() string {
if sf.value == nil {
return ""
}
return fmt.Sprintf("%f", *sf.value)
}

func (sf *float64PtrFlag) Type() string {
return "float64"
}

0 comments on commit 4acc991

Please sign in to comment.