-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restful_{resource|operation}
: precheck(_xxx)
supports api
and `…
…mutex` (#55)
- Loading branch information
Showing
10 changed files
with
591 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
terraform { | ||
required_providers { | ||
restful = { | ||
source = "magodo/restful" | ||
} | ||
} | ||
} | ||
|
||
variable "client_id" { | ||
type = string | ||
} | ||
|
||
variable "client_secret" { | ||
type = string | ||
} | ||
|
||
variable "tenant_id" { | ||
type = string | ||
} | ||
|
||
variable "subscription_id" { | ||
type = string | ||
} | ||
|
||
provider "restful" { | ||
base_url = "https://management.azure.com" | ||
security = { | ||
oauth2 = { | ||
client_credentials = { | ||
client_id = var.client_id | ||
client_secret = var.client_secret | ||
token_url = format("https://login.microsoftonline.com/%s/oauth2/v2.0/token", var.tenant_id) | ||
scopes = ["https://management.azure.com/.default"] | ||
} | ||
} | ||
} | ||
create_method = "PUT" | ||
} | ||
|
||
resource "restful_resource" "rg" { | ||
path = format("/subscriptions/%s/resourceGroups/%s", var.subscription_id, "example") | ||
query = { | ||
api-version = ["2020-06-01"] | ||
} | ||
poll_delete = { | ||
status_locator = "code" | ||
status = { | ||
success = "404" | ||
pending = ["202", "200"] | ||
} | ||
} | ||
body = jsonencode({ | ||
location = "westus" | ||
tags = { | ||
foo = "bar" | ||
} | ||
}) | ||
} | ||
|
||
locals { | ||
poll = { | ||
status_locator = "body.status" | ||
status = { | ||
success = "Succeeded" | ||
failure = "Failed" | ||
pending = ["Pending"] | ||
} | ||
url_locator = "header.azure-asyncoperation" | ||
} | ||
route_precheck = [ | ||
{ | ||
mutex = restful_resource.table.id | ||
} | ||
] | ||
} | ||
|
||
resource "restful_resource" "table" { | ||
path = format("%s/providers/Microsoft.Network/routeTables/%s", restful_resource.rg.id, "example") | ||
update_method = "PATCH" # This avoids update to clean up the routes in this table | ||
query = { | ||
api-version = ["2022-07-01"] | ||
} | ||
body = jsonencode({ | ||
location = "westus" | ||
}) | ||
poll_create = local.poll | ||
poll_delete = local.poll | ||
} | ||
|
||
resource "restful_resource" "route1" { | ||
path = format("%s/routes/%s", restful_resource.table.id, "route1") | ||
query = { | ||
api-version = ["2022-07-01"] | ||
} | ||
|
||
precheck_create = local.route_precheck | ||
precheck_update = local.route_precheck | ||
precheck_delete = local.route_precheck | ||
|
||
poll_create = local.poll | ||
poll_update = local.poll | ||
poll_delete = local.poll | ||
|
||
body = jsonencode({ | ||
properties = { | ||
nextHopType = "VnetLocal" | ||
addressPrefix = "10.1.0.0/16" | ||
} | ||
}) | ||
} | ||
|
||
resource "restful_resource" "route2" { | ||
path = format("%s/routes/%s", restful_resource.table.id, "route2") | ||
query = { | ||
api-version = ["2022-07-01"] | ||
} | ||
|
||
precheck_create = local.route_precheck | ||
precheck_update = local.route_precheck | ||
precheck_delete = local.route_precheck | ||
|
||
poll_create = local.poll | ||
poll_update = local.poll | ||
poll_delete = local.poll | ||
|
||
body = jsonencode({ | ||
properties = { | ||
nextHopType = "VnetLocal" | ||
addressPrefix = "10.2.0.0/16" | ||
} | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package locks | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type mutexKV struct { | ||
lock sync.Mutex | ||
store map[string]*sync.Mutex | ||
} | ||
|
||
func (m *mutexKV) Lock(ctx context.Context, key string) error { | ||
log.Printf("[DEBUG] Locking %q", key) | ||
l := m.get(key) | ||
for locked := false; !locked; { | ||
if err := ctx.Err(); err != nil { | ||
return err | ||
} | ||
locked = l.TryLock() | ||
} | ||
log.Printf("[DEBUG] Locked %q", key) | ||
return nil | ||
} | ||
|
||
func (m *mutexKV) Unlock(key string) { | ||
log.Printf("[DEBUG] Unlocking %q", key) | ||
m.get(key).Unlock() | ||
log.Printf("[DEBUG] Unlocked %q", key) | ||
} | ||
|
||
func (m *mutexKV) get(key string) *sync.Mutex { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
mutex, ok := m.store[key] | ||
if !ok { | ||
mutex = &sync.Mutex{} | ||
m.store[key] = mutex | ||
} | ||
return mutex | ||
} | ||
|
||
func NewMutexKV() *mutexKV { | ||
return &mutexKV{ | ||
store: make(map[string]*sync.Mutex), | ||
} | ||
} | ||
|
||
var monoMutexKV = NewMutexKV() | ||
|
||
func Lock(ctx context.Context, key string) error { | ||
return monoMutexKV.Lock(ctx, key) | ||
} | ||
|
||
func Unlock(key string) { | ||
monoMutexKV.Unlock(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.