Class: GeoserverMigrations::APIConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/geoserver_migrations/api_connector.rb

Instance Method Summary collapse

Constructor Details

#initializeAPIConnector

Returns a new instance of APIConnector.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/geoserver_migrations/api_connector.rb', line 4

def initialize
  # initialise geoserver-client
  GeoserverClient.api_root = ::GEOSERVER_MIGRATIONS_CONFIG[:geoserver_base]
  GeoserverClient.api_user = ::GEOSERVER_MIGRATIONS_CONFIG[:api][:user]
  GeoserverClient.api_password = ::GEOSERVER_MIGRATIONS_CONFIG[:api][:password]

  GeoserverClient.workspace    = GEOSERVER_MIGRATIONS_CONFIG[:workspace]
  GeoserverClient.datastore    = GEOSERVER_MIGRATIONS_CONFIG[:datastore]
  # if Rails.present?
  #   GeoserverClient.logger = Rails.logger
  # end
  GeoserverClient.logger = :stdout 
end

Instance Method Details

#execute(ordered_actions, direction = :up, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/geoserver_migrations/api_connector.rb', line 18

def execute(ordered_actions, direction = :up, options={})
  if direction != :up
    ordered_actions = ordered_actions.reverse
  end

  ordered_actions.each do |action_to_complete|
    case action_to_complete[:action]
      when :add_resource
        file_name = action_to_complete[:params][:name]
        if direction == :up
          GeoserverClient.create_resource file_name
        else
          GeoserverClient.delete_resource file_name
        end
      when :create_layer
        layer_name = action_to_complete[:params][:name]
        layer_config = action_to_complete[:params][:layer_config]
        if direction == :up
          # explicitly create style if sld given
          if !layer_config.sld.nil?
            puts " -- Create style #{layer_config.style_name}"

            GeoserverClient.create_style layer_config.style_name, sld: layer_config.sld
          end

          puts " -- Create layer #{layer_config.layer_name} [native_name = #{layer_config.feature_name}]"
          GeoserverClient.create_featuretype layer_name, native_name: layer_config.feature_name
          GeoserverClient.set_layer_style layer_name, layer_config.style_name
        else
          puts " -- delete layer #{layer_config.layer_name}"
          GeoserverClient.delete_featuretype layer_name

          if !layer_config.sld.nil?
            puts " -- delete style #{layer_config.style_name}"

            GeoserverClient.delete_style layer_config.style_name
          end

        end
      when :delete_layer
        if direction == :up
          layer_name = action_to_complete[:params][:name]
          puts " -- Delete layer #{layer_name}"
          GeoserverClient.delete_featuretype layer_name
        else
          # do nothing??
          # we should save the layer-definition in the :up direction so we can
          # restore it if needed?
        end
      when :delete_style
        if direction == :up
          style_name = action_to_complete[:params][:name]
          puts " -- Delete style #{style_name}"
          GeoserverClient.delete_style style_name
        end
      when :update_style
        if direction == :up
          style_name = action_to_complete[:params][:name]
          layer_config = action_to_complete[:params][:layer_config]
          puts " -- Updating style #{style_name}"
          GeoserverClient.update_style style_name, sld: layer_config.sld
        end
      when :create_layergroup
        layer_name = action_to_complete[:params][:name]
        layer_config = action_to_complete[:params][:layer_config]
        if direction == :up
          GeoserverClient.create_layergroup layer_name, layer_config.layers
        else
          puts " -- delete layergroup #{layer_config.layer_name}"
          GeoserverClient.delete_layergroup layer_name
        end
      when :delete_layergroup
        if direction == :up
          layer_name = action_to_complete[:params][:name]
          puts " -- Delete layergroup #{layer_name}"
          GeoserverClient.delete_layergroup layer_name
        else
          # do nothing??
          # we should save the layer-definition in the :up direction so we can
          # restore it if needed?
        end

     end
  end
end

#testObject



104
105
106
107
# File 'lib/geoserver_migrations/api_connector.rb', line 104

def test
  layers = GeoserverClient.layers
  layers["featureTypes"]["featureType"].map{|l| l["name"]}
end