Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Component Types #510

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20250127-150003.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: Add ability to CRUD 'ComponentType'
time: 2025-01-27T15:00:03.646584-06:00
107 changes: 107 additions & 0 deletions component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package opslevel

// ComponentTypeId Information about a particular component type
type ComponentTypeId Identifier

// ComponentType Information about a particular component type
type ComponentType struct {
ComponentTypeId
Description string // The description of the component type (Optional)
Href string // The relative path to link to the component type (Required)
Icon ComponentTypeIcon // The icon associated with the component type (Required)
IsDefault bool // Whether or not the component type is the default (Required)
Name string // The name of the component type (Required)
Timestamps Timestamps // When the component type was created and updated (Required)
}

// ComponentTypeIcon The icon for a component type
type ComponentTypeIcon struct {
Color string // The color, represented as a hexcode, for the icon (Optional)
Name string // The name of the icon in Phosphor icons for Vue, e.g. `PhBird`. See https://phosphoricons.com/ for a full list (Optional)
}

type ComponentTypeConnection struct {
Nodes []ComponentType `json:"nodes"`
PageInfo PageInfo `json:"pageInfo"`
TotalCount int `json:"totalCount" graphql:"-"`
}

func (client *Client) CreateComponentType(input ComponentTypeInput) (*ComponentType, error) {
var m struct {
Payload struct {
ComponentType ComponentType
Errors []OpsLevelErrors
} `graphql:"componentTypeCreate(input:$input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("ComponentTypeCreate"))
return &m.Payload.ComponentType, HandleErrors(err, m.Payload.Errors)
}

func (client *Client) GetComponentType(identifier string) (*ComponentType, error) {
var q struct {
Account struct {
ComponentType ComponentType `graphql:"componentType(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := client.Query(&q, v, WithName("ComponentTypeGet"))
return &q.Account.ComponentType, HandleErrors(err, nil)
}

func (client *Client) ListComponentTypes(variables *PayloadVariables) (*ComponentTypeConnection, error) {
var q struct {
Account struct {
ComponentTypes ComponentTypeConnection `graphql:"componentTypes(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("ComponentTypeList")); err != nil {
return nil, err
}
for q.Account.ComponentTypes.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.ComponentTypes.PageInfo.End
resp, err := client.ListComponentTypes(variables)
if err != nil {
return nil, err
}
q.Account.ComponentTypes.Nodes = append(q.Account.ComponentTypes.Nodes, resp.Nodes...)
q.Account.ComponentTypes.PageInfo = resp.PageInfo
}
q.Account.ComponentTypes.TotalCount = len(q.Account.ComponentTypes.Nodes)
return &q.Account.ComponentTypes, nil
}

func (client *Client) UpdateComponentType(identifier string, input ComponentTypeInput) (*ComponentType, error) {
var m struct {
Payload struct {
ComponentType ComponentType
Errors []OpsLevelErrors
} `graphql:"componentTypeUpdate(componentType:$target,input:$input)"`
}
v := PayloadVariables{
"target": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&m, v, WithName("ComponentTypeUpdate"))
return &m.Payload.ComponentType, HandleErrors(err, m.Payload.Errors)
}

func (client *Client) DeleteComponentType(identifier string) error {
var d struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"componentTypeDelete(resource:$target)"`
}
v := PayloadVariables{
"target": *NewIdentifier(identifier),
}
err := client.Mutate(&d, v, WithName("ComponentTypeDelete"))
return HandleErrors(err, d.Payload.Errors)
}
113 changes: 113 additions & 0 deletions component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package opslevel_test

import (
"testing"

ol "github.com/opslevel/opslevel-go/v2024"
"github.com/rocktavious/autopilot/v2023"
)

func TestComponentTypeCreate(t *testing.T) {
// Arrange
input := autopilot.Register[ol.ComponentTypeInput]("component_type_create_input",
ol.ComponentTypeInput{
Alias: ol.RefOf("example"),
Name: ol.RefOf("Example"),
Description: ol.RefOf("Example Description"),
Properties: &[]ol.ComponentTypePropertyDefinitionInput{},
})

testRequest := autopilot.NewTestRequest(
`mutation ComponentTypeCreate($input:ComponentTypeInput!){componentTypeCreate(input:$input){componentType{{ template "component_type_graphql" }},errors{message,path}}}`,
`{"input": {"alias": "example", "name": "Example", "description": "Example Description", "properties": []} }`,
`{"data": {"componentTypeCreate": {"componentType": {{ template "component_type_1_response" }} }}}`,
)

client := BestTestClient(t, "ComponentType/create", testRequest)
// Act
result, err := client.CreateComponentType(input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, id1, result.Id)
}

func TestComponentTypeGet(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`query ComponentTypeGet($input:IdentifierInput!){account{componentType(input: $input){{ template "component_type_graphql" }}}}`,
`{"input": { {{ template "id1" }} }}`,
`{"data": {"account": {"componentType": {{ template "component_type_1_response" }} }}}`,
)

client := BestTestClient(t, "ComponentType/get", testRequest)
// Act
result, err := client.GetComponentType(string(id1))
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, id1, result.Id)
}

func TestComponentTypeList(t *testing.T) {
// Arrange
testRequestOne := autopilot.NewTestRequest(
`query ComponentTypeList($after:String!$first:Int!){account{componentTypes(after: $after, first: $first){nodes{{ template "component_type_graphql" }},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor}}}}`,
`{{ template "pagination_initial_query_variables" }}`,
`{ "data": { "account": { "componentTypes": { "nodes": [ {{ template "component_type_1_response" }}, {{ template "component_type_2_response" }} ], {{ template "pagination_initial_pageInfo_response" }} }}}}`,
)
testRequestTwo := autopilot.NewTestRequest(
`query ComponentTypeList($after:String!$first:Int!){account{componentTypes(after: $after, first: $first){nodes{{ template "component_type_graphql" }},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor}}}}`,
`{{ template "pagination_second_query_variables" }}`,
`{ "data": { "account": { "componentTypes": { "nodes": [ {{ template "component_type_3_response" }} ], {{ template "pagination_second_pageInfo_response" }} }}}}`,
)
requests := []autopilot.TestRequest{testRequestOne, testRequestTwo}

client := BestTestClient(t, "ComponentType/list", requests...)
// Act
response, err := client.ListComponentTypes(nil)
result := response.Nodes
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, 3, response.TotalCount)
autopilot.Equals(t, "Example1", result[0].Name)
autopilot.Equals(t, "Example2", result[1].Name)
autopilot.Equals(t, "Example3", result[2].Name)
}

func TestComponentTypeUpdate(t *testing.T) {
// Arrange
input := autopilot.Register[ol.ComponentTypeInput]("component_type_update_input",
ol.ComponentTypeInput{
Alias: ol.RefOf("example"),
Name: ol.RefOf("Example"),
Description: ol.RefOf("Example Description"),
Properties: &[]ol.ComponentTypePropertyDefinitionInput{},
})

testRequest := autopilot.NewTestRequest(
`mutation ComponentTypeUpdate($input:ComponentTypeInput!$target:IdentifierInput!){componentTypeUpdate(componentType:$target,input:$input){componentType{{ template "component_type_graphql" }},errors{message,path}}}`,
`{"input": {"alias": "example", "name": "Example", "description": "Example Description", "properties": []}, "target": { {{ template "id1" }} }}`,
`{"data": {"componentTypeUpdate": {"componentType": {{ template "component_type_1_response" }} }}}`,
)

client := BestTestClient(t, "ComponentType/update", testRequest)
// Act
result, err := client.UpdateComponentType(string(id1), input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, id1, result.Id)
}

func TestComponentTypeDelete(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation ComponentTypeDelete($target:IdentifierInput!){componentTypeDelete(resource:$target){errors{message,path}}}`,
`{"target": { {{ template "id1" }} }}`,
`{"data": {"componentTypeDelete": {"errors": [] }}}`,
)

client := BestTestClient(t, "ComponentType/delete", testRequest)
// Act
err := client.DeleteComponentType(string(id1))
// Assert
autopilot.Ok(t, err)
}
Loading
Loading