parseConnector parses a connector definition and adds the resulting resource to p.Resources.
(node *Node)
| 18 | |
| 19 | // parseConnector parses a connector definition and adds the resulting resource to p.Resources. |
| 20 | func (p *Parser) parseConnector(node *Node) error { |
| 21 | // Parse YAML |
| 22 | tmp := &ConnectorYAML{} |
| 23 | err := p.decodeNodeYAML(node, false, tmp) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | |
| 28 | // "Managed" indicates that we should automatically provision the connector |
| 29 | var provision bool |
| 30 | var provisionArgsPB *structpb.Struct |
| 31 | if !tmp.Managed.IsZero() { |
| 32 | switch tmp.Managed.Kind { |
| 33 | case yaml.ScalarNode: |
| 34 | err := tmp.Managed.Decode(&provision) |
| 35 | if err != nil { |
| 36 | return fmt.Errorf("failed to decode 'managed': %w", err) |
| 37 | } |
| 38 | case yaml.MappingNode: |
| 39 | provision = true |
| 40 | var provisionArgs map[string]any |
| 41 | err := tmp.Managed.Decode(&provisionArgs) |
| 42 | if err != nil { |
| 43 | return fmt.Errorf("failed to decode 'managed': %w", err) |
| 44 | } |
| 45 | provisionArgsPB, err = structpb.NewStruct(provisionArgs) |
| 46 | if err != nil { |
| 47 | return fmt.Errorf("failed to convert provision args to proto: %w", err) |
| 48 | } |
| 49 | default: |
| 50 | return fmt.Errorf("invalid type for 'managed': expected boolean or map of args") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Find out if any properties are templated |
| 55 | templatedProps, err := analyzeTemplatedProperties(tmp.Properties) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("failed to analyze templated properties: %w", err) |
| 58 | } |
| 59 | var propertiesPB *structpb.Struct |
| 60 | if tmp.Properties != nil { |
| 61 | propertiesPB, err = structpb.NewStruct(tmp.Properties) |
| 62 | if err != nil { |
| 63 | return fmt.Errorf("failed to convert connector properties to proto: %w", err) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Insert the connector |
| 68 | r, err := p.insertResource(ResourceKindConnector, node.Name, node.Paths, node.Refs...) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | // NOTE: After calling insertResource, an error must not be returned. Any validation should be done before calling it. |
| 73 | |
| 74 | r.ConnectorSpec.Driver = tmp.Driver |
| 75 | r.ConnectorSpec.Properties = propertiesPB |
| 76 | r.ConnectorSpec.TemplatedProperties = templatedProps |
| 77 | r.ConnectorSpec.Provision = provision |
no test coverage detected