Skip to content

Commit

Permalink
feat: add publish monograph feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus committed Nov 14, 2024
1 parent abea691 commit 108d13a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/guides/cosmo-monograph-contract/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module "cosmo_monograph" {
monograph_namespace = module.cosmo_namespace.name
monograph_graph_url = var.monograph_graph_url
monograph_routing_url = var.monograph_routing_url
monograph_schema = var.monograph_schema
}

module "cosmo_contract" {
Expand Down
4 changes: 4 additions & 0 deletions examples/guides/cosmo-monograph-contract/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ variable "monograph_routing_url" {
default = "http://example.com/routing"
}

variable "monograph_schema" {
default = "type Query{ a: String }"
}

variable "contract_name" {
type = string
default = "test"
Expand Down
1 change: 1 addition & 0 deletions examples/resources/cosmo_monograph/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ resource "cosmo_monograph" "example" {
namespace = var.monograph_namespace
graph_url = var.monograph_graph_url
routing_url = var.monograph_routing_url
schema = var.monograph_schema
}
4 changes: 4 additions & 0 deletions examples/resources/cosmo_monograph/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ variable "monograph_graph_url" {

variable "monograph_routing_url" {
default = "http://example.com/routing"
}

variable "monograph_schema" {
default = "type Query{ a: String }"
}
23 changes: 23 additions & 0 deletions internal/api/monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,26 @@ func (p PlatformClient) GetMonograph(ctx context.Context, name string, namespace

return response.Msg.Graph, nil
}

func (p PlatformClient) PublishMonograph(ctx context.Context, name string, namespace string, schema string) *ApiError {
request := connect.NewRequest(&platformv1.PublishMonographRequest{
Name: name,
Namespace: namespace,
Schema: schema,
})
response, err := p.Client.PublishMonograph(ctx, request)
if err != nil {
return &ApiError{Err: err, Reason: "PublishMonograph", Status: common.EnumStatusCode_ERR}
}

if response.Msg == nil {
return &ApiError{Err: ErrEmptyMsg, Reason: "PublishMonograph", Status: common.EnumStatusCode_ERR}
}

apiError := handleErrorCodes(response.Msg.GetResponse().Code, response.Msg.String())
if apiError != nil {
return apiError
}

return nil
}
1 change: 1 addition & 0 deletions internal/service/monograph/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package monograph
const (
ErrInvalidMonographName = "Invalid Monograph Name"
ErrCreatingMonograph = "Error Creating Monograph"
ErrPublishingMonograph = "Error Publishing Monograph"
ErrCompositionError = "Composition Error"
ErrRetrievingMonograph = "Error Retrieving Monograph"
ErrInvalidResourceID = "Invalid Resource ID"
Expand Down
64 changes: 59 additions & 5 deletions internal/service/monograph/resource_cosmo_monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type MonographResourceModel struct {
Readme types.String `tfsdk:"readme"`
AdmissionWebhookURL types.String `tfsdk:"admission_webhook_url"`
AdmissionWebhookSecret types.String `tfsdk:"admission_webhook_secret"`
Schema types.String `tfsdk:"schema"`
}

func NewMonographResource() resource.Resource {
Expand Down Expand Up @@ -112,6 +113,10 @@ For more information on monographs, please refer to the [Cosmo Documentation](ht
stringvalidator.OneOf(api.GraphQLSubscriptionProtocolWS, api.GraphQLSubscriptionProtocolSSE, api.GraphQLSubscriptionProtocolSSEPost),
},
},
"schema": schema.StringAttribute{
Optional: true,
MarkdownDescription: "The schema for the subgraph.",
},
},
}
}
Expand Down Expand Up @@ -170,6 +175,26 @@ func (r *MonographResource) Create(ctx context.Context, req resource.CreateReque
return
}

if data.Schema.ValueString() != "" {
err := r.client.PublishMonograph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString())
if err != nil {
if api.IsNotFoundError(err) {
utils.AddDiagnosticError(resp,
ErrPublishingMonograph,
err.Error(),
)
resp.State.RemoveResource(ctx)
return
} else {
utils.AddDiagnosticError(resp,
ErrPublishingMonograph,
err.Error(),
)
return
}
}
}

monograph, apiError := r.client.GetMonograph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
if apiError != nil {
utils.AddDiagnosticError(resp,
Expand Down Expand Up @@ -250,11 +275,40 @@ func (r *MonographResource) Update(ctx context.Context, req resource.UpdateReque
data.AdmissionWebhookSecret.ValueString(),
)
if err != nil {
utils.AddDiagnosticError(resp,
ErrUpdatingMonograph,
err.Error(),
)
return
if api.IsNotFoundError(err) {
utils.AddDiagnosticError(resp,
ErrUpdatingMonograph,
err.Error(),
)
resp.State.RemoveResource(ctx)
return
} else {
utils.AddDiagnosticError(resp,
ErrUpdatingMonograph,
err.Error(),
)
return
}
}

if data.Schema.ValueString() != "" {
err := r.client.PublishMonograph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString())
if err != nil {
if api.IsNotFoundError(err) {
utils.AddDiagnosticError(resp,
ErrUpdatingMonograph,
err.Error(),
)
resp.State.RemoveResource(ctx)
return
} else {
utils.AddDiagnosticError(resp,
ErrUpdatingMonograph,
err.Error(),
)
return
}
}
}

utils.LogAction(ctx, "updated monograph", data.Id.ValueString(), data.Name.ValueString(), data.Namespace.ValueString())
Expand Down

0 comments on commit 108d13a

Please sign in to comment.