Skip to content

Commit

Permalink
New resource: azurerm_orbital_contact (#19036)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaweitao001 authored Jan 26, 2023
1 parent 7866ffc commit bc1e094
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 0 deletions.
207 changes: 207 additions & 0 deletions internal/services/orbital/contact_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package orbital

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/orbital/2022-03-01/contact"
"github.com/hashicorp/go-azure-sdk/resource-manager/orbital/2022-03-01/contactprofile"
"github.com/hashicorp/go-azure-sdk/resource-manager/orbital/2022-03-01/spacecraft"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type ContactResource struct{}

type ContactResourceModel struct {
Name string `tfschema:"name"`
Spacecraft string `tfschema:"spacecraft_id"`
ReservationStartTime string `tfschema:"reservation_start_time"`
ReservationEndTime string `tfschema:"reservation_end_time"`
GroundStationName string `tfschema:"ground_station_name"`
ContactProfileId string `tfschema:"contact_profile_id"`
}

func (r ContactResource) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"spacecraft_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: spacecraft.ValidateSpacecraftID,
},

"reservation_start_time": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"reservation_end_time": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"ground_station_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"contact_profile_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: contactprofile.ValidateContactProfileID,
},
}
}

func (r ContactResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{}
}

func (r ContactResource) ModelObject() interface{} {
return &ContactResourceModel{}
}

func (r ContactResource) ResourceType() string {
return "azurerm_orbital_contact"
}

func (r ContactResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model ContactResourceModel
if err := metadata.Decode(&model); err != nil {
return err
}

client := metadata.Client.Orbital.ContactClient
subscriptionId := metadata.Client.Account.SubscriptionId

spacecraftId, err := contact.ParseSpacecraftID(model.Spacecraft)
if err != nil {
return err
}
id := contact.NewContactID(subscriptionId, spacecraftId.ResourceGroupName, spacecraftId.SpacecraftName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}

if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

contactProfile := contact.ResourceReference{
Id: &model.ContactProfileId,
}

contactProperties := contact.ContactsProperties{
ContactProfile: contactProfile,
GroundStationName: model.GroundStationName,
ReservationEndTime: model.ReservationEndTime,
ReservationStartTime: model.ReservationStartTime,
}

contact := contact.Contact{
Id: utils.String(id.ID()),
Name: utils.String(model.Name),
Properties: &contactProperties,
}
if _, err = client.Create(ctx, id, contact); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
metadata.SetID(id)
return nil
},
}
}

func (r ContactResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Orbital.ContactClient
id, err := contact.ParseContactID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("reading %s: %+v", *id, err)
}

spacecraftId := contact.NewSpacecraftID(id.SubscriptionId, id.ResourceGroupName, id.SpacecraftName)
if model := resp.Model; model != nil {
props := model.Properties
state := ContactResourceModel{
Name: id.ContactName,
Spacecraft: spacecraftId.ID(),
}

if props != nil {
state.ReservationStartTime = props.ReservationStartTime
state.ReservationEndTime = props.ReservationEndTime
state.GroundStationName = props.GroundStationName
if props.ContactProfile.Id != nil {
state.ContactProfileId = *props.ContactProfile.Id
} else {
return fmt.Errorf("contact profile id is missing %s", *id)
}
} else {
return fmt.Errorf("required properties are missing %s", *id)
}

return metadata.Encode(&state)
}
return nil
},
}
}

func (r ContactResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Orbital.ContactClient
id, err := contact.ParseContactID(metadata.ResourceData.Id())
if err != nil {
return err
}

metadata.Logger.Infof("deleting %s", *id)

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
return nil
},
}
}

func (r ContactResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return contact.ValidateContactID
}
139 changes: 139 additions & 0 deletions internal/services/orbital/contact_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package orbital_test

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/orbital/2022-03-01/contact"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type ContactResource struct{}

func TestAccContact_basic(t *testing.T) {
skipContact(t)

data := acceptance.BuildTestData(t, "azurerm_orbital_contact", "test")
r := ContactResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r ContactResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := contact.ParseContactID(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Orbital.ContactClient.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}
return utils.Bool(true), nil
}

func (r ContactResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%[1]s
resource "azurerm_orbital_contact" "test" {
name = "testcontact-%[2]d"
spacecraft_id = "%[3]s"
reservation_start_time = "2025-07-16T20:35:00Z"
reservation_end_time = "2025-07-16T20:55:00Z"
ground_station_name = "Microsoft_Quincy"
contact_profile_id = azurerm_orbital_contact_profile.test.id
}
`, template, data.RandomInteger, os.Getenv("ARM_TEST_SPACECRAFT_ID"))
}

func (r ContactResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}
resource "azurerm_virtual_network" "test" {
name = "testvnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "testsubnet"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "orbitalgateway"
service_delegation {
name = "Microsoft.Orbital/orbitalGateways"
actions = [
"Microsoft.Network/publicIPAddresses/join/action",
"Microsoft.Network/virtualNetworks/subnets/join/action",
"Microsoft.Network/virtualNetworks/read",
"Microsoft.Network/publicIPAddresses/read",
]
}
}
}
resource "azurerm_orbital_contact_profile" "test" {
name = "testcontactprofile-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
minimum_variable_contact_duration = "PT1M"
auto_tracking = "disabled"
links {
channels {
name = "channelname"
bandwidth_mhz = 100
center_frequency_mhz = 101
end_point {
end_point_name = "AQUA_command"
ip_address = "10.0.1.0"
port = "49153"
protocol = "TCP"
}
}
direction = "Uplink"
name = "RHCP_UL"
polarization = "RHCP"
}
network_configuration_subnet_id = azurerm_subnet.test.id
}
`, data.RandomInteger, data.Locations.Primary)
}

func skipContact(t *testing.T) {
if os.Getenv("ARM_TEST_SPACECRAFT_ID") == "" {
t.Skip("Skipping as `ARM_TEST_SPACECRAFT_ID` was not specified")
}
}
1 change: 1 addition & 0 deletions internal/services/orbital/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
SpacecraftResource{},
ContactProfileResource{},
ContactResource{},
}
}

Expand Down
Loading

0 comments on commit bc1e094

Please sign in to comment.