Skip to content

Commit

Permalink
Update backup api with constants
Browse files Browse the repository at this point in the history
Signed-off-by: pritamdas99 <[email protected]>
  • Loading branch information
pritamdas99 committed Jul 17, 2024
1 parent 323695e commit 4eb1785
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 119 deletions.
18 changes: 18 additions & 0 deletions solr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import (
"github.com/go-resty/resty/v2"
)

const (
writeCollectionName = "kubedb-system"
Action = "action"
ActionBackup = "BACKUP"
ActionRestore = "RESTORE"
ActionDeleteBackup = "DELETEBACKUP"
BackupName = "name"
Location = "location"
Repository = "repository"
Collection = "collection"
Async = "async"
PurgeUnused = "purgeUnused"
BackupId = "backupId"
DeleteStatus = "DELETESTATUS"
RequestStatus = "REQUESTSTATUS"
RequestId = "requestid"
)

type SLClient interface {
GetClusterStatus() (*Response, error)
ListCollection() (*Response, error)
Expand Down
14 changes: 0 additions & 14 deletions solr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ type QueryParams struct {
Limit int `json:"limit,omitempty" yaml:"limit,omitempty"`
}

type BackupParams struct {
Location string `json:"location,omitempty" yaml:"location,omitempty"`
Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`
Async string `json:"async,omitempty" yaml:"async,omitempty"`
}

type BackupRestoreParams struct {
Location string `json:"location,omitempty" yaml:"location,omitempty"`
Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`
Collection string `json:"collection,omitempty" yaml:"collection,omitempty"`
Async string `json:"async,omitempty" yaml:"async,omitempty"`
BackupId int `json:"backupId,omitempty" yaml:"backupId,omitempty"`
}

type CreateParams struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Config string `json:"config,omitempty" yaml:"config,omitempty"`
Expand Down
18 changes: 8 additions & 10 deletions solr/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func (o *KubeDBClientBuilder) GetSolrClient() (*Client, error) {
config := Config{
host: o.url,
transport: &http.Transport{
IdleConnTimeout: time.Minute * 6,
IdleConnTimeout: time.Second * 10,
DialContext: (&net.Dialer{
Timeout: time.Minute * 6,
KeepAlive: time.Minute * 6,
Timeout: time.Second * 30,
KeepAlive: time.Second * 30,
}).DialContext,
TLSHandshakeTimeout: time.Minute * 6,
ResponseHeaderTimeout: time.Minute * 6,
ExpectContinueTimeout: time.Minute * 6,
TLSHandshakeTimeout: time.Second * 20,
ResponseHeaderTimeout: time.Second * 20,
ExpectContinueTimeout: time.Second * 20,
},
connectionScheme: o.db.GetConnectionScheme(),
log: o.log,
Expand All @@ -103,28 +103,26 @@ func (o *KubeDBClientBuilder) GetSolrClient() (*Client, error) {
case version.Major() == 9:
newClient := resty.New()
newClient.SetScheme(config.connectionScheme).SetBaseURL(config.host).SetTransport(config.transport)
newClient.SetTimeout(6 * time.Minute)
newClient.SetTimeout(time.Second * 30)
newClient.SetHeader("Accept", "application/json")
newClient.SetDisableWarn(true)
newClient.SetBasicAuth(string(authSecret.Data[core.BasicAuthUsernameKey]), string(authSecret.Data[core.BasicAuthPasswordKey]))
return &Client{
&SLClientV9{
Client: newClient,
log: config.log,
Config: &config,
},
}, nil
case version.Major() == 8:
newClient := resty.New()
newClient.SetScheme(config.connectionScheme).SetBaseURL(config.host).SetTransport(config.transport)
newClient.SetTimeout(6 * time.Minute)
newClient.SetTimeout(time.Second * 30)
newClient.SetHeader("Accept", "application/json")
newClient.SetDisableWarn(true)
newClient.SetBasicAuth(string(authSecret.Data[core.BasicAuthUsernameKey]), string(authSecret.Data[core.BasicAuthPasswordKey]))
return &Client{
&SLClientV8{
Client: newClient,
log: config.log,
Config: &config,
},
}, nil
Expand Down
98 changes: 48 additions & 50 deletions solr/solrv8.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

type SLClientV8 struct {
Client *resty.Client
log logr.Logger
Config *Config
}

Expand All @@ -23,7 +22,7 @@ func (sc *SLClientV8) GetClusterStatus() (*Response, error) {
req.SetQueryParam("action", "CLUSTERSTATUS")
res, err := req.Get("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request")
sc.Config.log.Error(err, "Failed to send http request")
return nil, err
}

Expand All @@ -36,12 +35,12 @@ func (sc *SLClientV8) GetClusterStatus() (*Response, error) {
}

func (sc *SLClientV8) ListCollection() (*Response, error) {
sc.Config.log.V(5).Info("SEARCHING COLLECTION: kubedb-system")
sc.Config.log.V(5).Info(fmt.Sprintf("SEARCHING COLLECTION: %s", writeCollectionName))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetQueryParam("action", "LIST")
res, err := req.Get("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request while getting colection list")
sc.Config.log.Error(err, "Failed to send http request while getting colection list")
return nil, err
}
response := &Response{
Expand All @@ -53,20 +52,20 @@ func (sc *SLClientV8) ListCollection() (*Response, error) {
}

func (sc *SLClientV8) CreateCollection() (*Response, error) {
sc.Config.log.V(5).Info("CREATING COLLECTION: kubedb-system")
sc.Config.log.V(5).Info(fmt.Sprintf("CREATING COLLECTION: %s", writeCollectionName))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetHeader("Content-Type", "application/json")
params := map[string]string{
"action": "CREATE",
"name": "kubedb-system",
"name": writeCollectionName,
"numShards": "1",
"replicationFactor": "1",
}

req.SetQueryParams(params)
res, err := req.Post("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to create a collection")
sc.Config.log.Error(err, "Failed to send http request to create a collection")
return nil, err
}

Expand All @@ -79,7 +78,7 @@ func (sc *SLClientV8) CreateCollection() (*Response, error) {
}

func (sc *SLClientV8) WriteCollection() (*Response, error) {
sc.Config.log.V(5).Info("WRITING COLLECTION: kubedb-system")
sc.Config.log.V(5).Info(fmt.Sprintf("WRITING COLLECTION: %s", writeCollectionName))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetHeader("Content-Type", "application/json")
data1 := &Data{
Expand All @@ -94,9 +93,9 @@ func (sc *SLClientV8) WriteCollection() (*Response, error) {
Add: data1,
}
req.SetBody(add)
res, err := req.Post("/solr/kubedb-system/update")
res, err := req.Post(fmt.Sprintf("/solr/%s/update", writeCollectionName))
if err != nil {
sc.log.Error(err, "Failed to send http request to add document in collect")
sc.Config.log.Error(err, "Failed to send http request to add document in collect")
return nil, err
}

Expand All @@ -109,12 +108,12 @@ func (sc *SLClientV8) WriteCollection() (*Response, error) {
}

func (sc *SLClientV8) ReadCollection() (*Response, error) {
sc.Config.log.V(5).Info("READING COLLECTION: kubedb-system")
sc.Config.log.V(5).Info(fmt.Sprintf("READING COLLECTION: %s", writeCollectionName))
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetQueryParam("q", "*:*")
res, err := req.Get("/solr/kubedb-system/select")
res, err := req.Get(fmt.Sprintf("/solr/%s/select", writeCollectionName))
if err != nil {
sc.log.Error(err, "Failed to send http request to read a collection")
sc.Config.log.Error(err, "Failed to send http request to read a collection")
return nil, err
}

Expand All @@ -131,19 +130,19 @@ func (sc *SLClientV8) BackupCollection(ctx context.Context, collection string, b
req := sc.Client.R().SetDoNotParseResponse(true).SetContext(ctx)
req.SetHeader("Content-Type", "application/json")
params := map[string]string{
"action": "BACKUP",
"name": backupName,
"collection": collection,
"location": location,
"repository": repository,
"async": fmt.Sprintf("%s-backup", collection),
Action: ActionBackup,
BackupName: backupName,
Collection: collection,
Location: location,
Repository: repository,
Async: fmt.Sprintf("%s-backup", collection),
}

req.SetQueryParams(params)

res, err := req.Post("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to backup a collection")
sc.Config.log.Error(err, "Failed to send http request to backup a collection")
return nil, err
}

Expand All @@ -160,20 +159,20 @@ func (sc *SLClientV8) RestoreCollection(ctx context.Context, collection string,
req := sc.Client.R().SetDoNotParseResponse(true).SetContext(ctx)
req.SetHeader("Content-Type", "application/json")
params := map[string]string{
"action": "RESTORE",
"name": backupName,
"collection": collection,
"location": location,
"repository": repository,
"backupId": strconv.Itoa(backupId),
"async": fmt.Sprintf("%s-restore", collection),
Action: ActionRestore,
BackupName: backupName,
Location: location,
Collection: collection,
Repository: repository,
BackupId: strconv.Itoa(backupId),
Async: fmt.Sprintf("%s-restore", collection),
}

req.SetQueryParams(params)

res, err := req.Post("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to restore a collection")
sc.Config.log.Error(err, "Failed to send http request to restore a collection")
return nil, err
}

Expand All @@ -191,13 +190,13 @@ func (sc *SLClientV8) FlushStatus(asyncId string) (*Response, error) {
req.SetHeader("Content-Type", "application/json")

params := map[string]string{
"action": "DELETESTATUS",
"requestid": asyncId,
Action: DeleteStatus,
RequestId: asyncId,
}
req.SetQueryParams(params)
res, err := req.Get("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to flush status")
sc.Config.log.Error(err, "Failed to send http request to flush status")
return nil, err
}

Expand All @@ -214,13 +213,13 @@ func (sc *SLClientV8) RequestStatus(asyncId string) (*Response, error) {
req := sc.Client.R().SetDoNotParseResponse(true)
req.SetHeader("Content-Type", "application/json")
params := map[string]string{
"action": "REQUESTSTATUS",
"requestid": asyncId,
Action: RequestStatus,
RequestId: asyncId,
}
req.SetQueryParams(params)
res, err := req.Get("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to request status")
sc.Config.log.Error(err, "Failed to send http request to request status")
return nil, err
}
backupResponse := &Response{
Expand All @@ -240,19 +239,18 @@ func (sc *SLClientV8) DeleteBackup(ctx context.Context, backupName string, colle
async = fmt.Sprintf("%s-%s", async, snap)
}
params := map[string]string{
"action": "DELETEBACKUP",
"name": backupName,
"location": location,
"repository": repository,
"backupId": strconv.Itoa(backupId),
"purgeUnused": "true",
"async": async,
Action: ActionDeleteBackup,
BackupName: backupName,
Location: location,
Repository: repository,
BackupId: strconv.Itoa(backupId),
Async: async,
}
req.SetQueryParams(params)

res, err := req.Delete("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to restore a collection")
sc.Config.log.Error(err, "Failed to send http request to restore a collection")
return nil, err
}

Expand All @@ -273,17 +271,17 @@ func (sc *SLClientV8) PurgeBackup(ctx context.Context, backupName string, collec
async = fmt.Sprintf("%s-%s", async, snap)
}
params := map[string]string{
"action": "DELETEBACKUP",
"name": backupName,
"location": location,
"repository": repository,
"purgeUnused": "true",
"async": async,
Action: ActionDeleteBackup,
BackupName: backupName,
Location: location,
Repository: repository,
PurgeUnused: "true",
Async: async,
}
req.SetQueryParams(params)
res, err := req.Put("/solr/admin/collections")
if err != nil {
sc.log.Error(err, "Failed to send http request to restore a collection")
sc.Config.log.Error(err, "Failed to send http request to restore a collection")
return nil, err
}

Expand All @@ -304,7 +302,7 @@ func (sc *SLClientV8) GetClient() *resty.Client {
}

func (sc *SLClientV8) GetLog() logr.Logger {
return sc.log
return sc.Config.log
}

func (sc *SLClientV8) DecodeBackupResponse(data map[string]interface{}, collection string) ([]byte, error) {
Expand Down
Loading

0 comments on commit 4eb1785

Please sign in to comment.