Skip to content

Commit

Permalink
db
Browse files Browse the repository at this point in the history
  • Loading branch information
William Mortl authored and William Mortl committed Apr 2, 2020
1 parent 0222326 commit cb2bf20
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 286 deletions.
264 changes: 0 additions & 264 deletions pkg/resourcemanager/psql/database/client.go

This file was deleted.

125 changes: 125 additions & 0 deletions pkg/resourcemanager/psql/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package database

import (
"context"
"net/http"

psql "github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/iam"
)

type PSQLDatabaseClient struct {
}

func NewPSQLDatabaseClient() *PSQLDatabaseClient {
return &PSQLDatabaseClient{}
}

func getPSQLDatabasesClient() (psql.DatabasesClient, error) {
databasesClient := psql.NewDatabasesClientWithBaseURI(config.BaseURI(), config.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer()
if err != nil {
return psql.DatabasesClient{}, err
}
databasesClient.Authorizer = a
databasesClient.AddToUserAgent(config.UserAgent())
return databasesClient, err
}

func getPSQLCheckNameAvailabilityClient() (psql.CheckNameAvailabilityClient, error) {
nameavailabilityClient := psql.NewCheckNameAvailabilityClientWithBaseURI(config.BaseURI(), config.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer()
if err != nil {
return psql.CheckNameAvailabilityClient{}, err
}
nameavailabilityClient.Authorizer = a
nameavailabilityClient.AddToUserAgent(config.UserAgent())
return nameavailabilityClient, err
}

func (p *PSQLDatabaseClient) CheckDatabaseNameAvailability(ctx context.Context, databasename string) (bool, error) {

client, err := getPSQLCheckNameAvailabilityClient()
if err != nil {
return false, err
}

resourceType := "database"

nameAvailabilityRequest := psql.NameAvailabilityRequest{
Name: &databasename,
Type: &resourceType,
}
_, err = client.Execute(ctx, nameAvailabilityRequest)
if err == nil { // Name available
return true, nil
}
return false, err

}

func (p *PSQLDatabaseClient) CreateDatabaseIfValid(ctx context.Context, databasename string, servername string, resourcegroup string) (*http.Response, error) {

client, err := getPSQLDatabasesClient()
if err != nil {
return &http.Response{
StatusCode: 500,
}, err
}

// Check if name is valid if this is the first create call
valid, err := p.CheckDatabaseNameAvailability(ctx, databasename)
if valid == false {
return &http.Response{
StatusCode: 500,
}, err
}

dbParameters := psql.Database{}

future, err := client.CreateOrUpdate(
ctx,
resourcegroup,
servername,
databasename,
dbParameters,
)
if err != nil {
return &http.Response{
StatusCode: 500,
}, err
}

return future.GetResult(client)
}

func (p *PSQLDatabaseClient) DeleteDatabase(ctx context.Context, databasename string, servername string, resourcegroup string) (status string, err error) {

client, err := getPSQLDatabasesClient()
if err != nil {
return "", err
}

_, err = client.Get(ctx, resourcegroup, servername, databasename)
if err == nil { // db present, so go ahead and delete
future, err := client.Delete(ctx, resourcegroup, servername, databasename)
return future.Status(), err
}

// db not present so return success anyway
return "db not present", nil
}

func (p *PSQLDatabaseClient) GetDatabase(ctx context.Context, resourcegroup string, servername string, databasename string) (db psql.Database, err error) {

client, err := getPSQLDatabasesClient()
if err != nil {
return psql.Database{}, err
}

return client.Get(ctx, resourcegroup, servername, databasename)
}
Loading

0 comments on commit cb2bf20

Please sign in to comment.