예제 #1
0
// Validate -
func (e *Event) Validate() error {

	if !utils.StringInSlice(e.EventType, AvailableEventTypes) {
		return fmt.Errorf(
			"Could not validate (event: %v). (event_type: %s) is not under available (event_types: %v)",
			e, e.EventType, AvailableEventTypes,
		)
	}

	return nil
}
예제 #2
0
// Validate -
func (c *Connection) Validate() error {
	c.Info("Validating mqtt configuration for (worker: %q)", c.Name())

	if c.Config.Get("connection") == nil {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection interface is missing (entry: %s)",
			c.Config.Get("connection"),
		)
	}

	data := c.Config.Get("connection").(map[string]interface{})

	if _, ok := data["network"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection network is not set. (connection_data: %q)",
			data,
		)
	}

	if !utils.StringInSlice(data["network"].(string), AvailableConnectionTypes) {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection network is not valid. (network: %s) - (available_networks: %v)",
			data["network"].(string), AvailableConnectionTypes,
		)
	}

	if _, ok := data["address"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection address is not set. (connection_data: %q)",
			data,
		)
	}

	address := data["address"].(string)

	if len(address) < 5 || !strings.Contains(address, ":") {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection address is not valid. (address: %s)",
			address,
		)
	}

	if _, ok := data["username"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection username is not set. Username can be empty but it MUST be set. (connection_data: %q)",
			data,
		)
	}

	if _, ok := data["password"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection password is not set. Password can be empty but it MUST be set. (connection_data: %q)",
			data,
		)
	}

	clientID := data["clientId"].(string)

	if _, ok := data["clientId"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection clientId is not set. (client_id: %s)",
			clientID,
		)
	}

	if len(clientID) < 2 {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection clientId is not long enough. (client_id: %s)",
			clientID,
		)
	}

	if _, ok := data["topic"].(string); !ok {
		return fmt.Errorf(
			"Could not validate mqtt worker as connection topic is not set. (connection_data: %q)",
			data,
		)
	}

	return nil
}